import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/search_page/notifier/search_tag_notifier.dart';
import 'package:zhiying_base_widget/pages/search_result_page/search_result_page.dart';
import 'package:zhiying_base_widget/widgets/search/hot_tag/model/search_hot_tag_model.dart';
import 'package:zhiying_base_widget/widgets/search/widget/text_tag_widget.dart';
import 'package:zhiying_base_widget/widgets/search/widget/title_widget.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:provider/provider.dart';

///
/// 搜索页面，热门搜索的标签
///
class SearchHotTagWidget extends StatelessWidget {
  final Map<String, dynamic> data;
  SearchHotTagModel model;

  SearchHotTagWidget(this.data, {Key key}) : super(key: key) {
    try {
      model = SearchHotTagModel.fromJson(jsonDecode(data['data']));
    } catch (e) {
      Logger.error(e);
    }
  }

  /// 点击事件
  void _tagOnClick(BuildContext context, SearchHostTagItemModel model) async{
    print('${model?.keyword}');
    if (!EmptyUtil.isEmpty(model?.keyword)) {

      await Provider.of<SearchTagNotifier>(context, listen: false).addTag(model.keyword);

      Navigator.push(context, MaterialPageRoute(builder: (_) => SearchResultPage({'keywords': model?.keyword ?? ''})));
    }
  }

  @override
  Widget build(BuildContext context) {
    return _getMainWidget(context, model);
  }

  /// 获取主视图
  Widget _getMainWidget(BuildContext context, SearchHotTagModel model) {
    return Container(
      width: double.infinity,
      margin: const EdgeInsets.only(left: 12.5, right: 12.5, top: 15),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          /// 标题
          _getTitleWidget(model),
          const SizedBox(height: 13),
          _getTagWidget(context, model),
        ],
      ),
    );
  }

  /// 标签widget
  Widget _getTagWidget(BuildContext context, SearchHotTagModel model) {
    List<Widget> itemWidgetList = [];
    final int tagListLength = model?.search_hosts?.length ?? 0;
    if (tagListLength > 0) {
      for (var i = 0; i < tagListLength; i++) {
        var item = model.search_hosts[i];
        itemWidgetList.add(GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () => _tagOnClick(context, item),
          child: TextTagWidget(
            "${item?.keyword ?? ''}",
            padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 10),
            margin: const EdgeInsets.only(right: 5, bottom: 0, top: 0),
            borderColor: HexColor.fromHex(item?.keyword_background_color ?? '#FFFFFF'),
            backgroundColor: HexColor.fromHex(item?.keyword_background_color ?? '#FFFFFF'),
            textStyle: TextStyle(color: HexColor.fromHex(item?.keyword_color ?? '#383838'), fontSize: 12),
            icon: item?.hot_icon ?? null,
          ),
        ));
      }

      return Wrap(
          spacing: 8.0,
          runSpacing: 8.0,

          ///子标签
          children: itemWidgetList);
    }
    return Container();
  }

  /// 标题widget
  Widget _getTitleWidget(SearchHotTagModel model) {
    return SearchTitleWidget(
      titleText: model?.hot_title ?? '热门搜索',
      titleTextColor: model?.hot_title_color ?? '#333333',
    );
  }
}
