import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/team_page/model/team_style_model.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class TeamFilterWidget extends StatefulWidget {
  TeamStyleModel styleModel;

  TeamFilterWidget(this.styleModel);

  @override
  _TeamFilterWidgetState createState() => _TeamFilterWidgetState();
}

class _TeamFilterWidgetState extends State<TeamFilterWidget> {
  /// 特殊的筛选条件，与其它并存
  static final String SPECIAL_TYPE = 'relate';

  void _onClick(TeamViewSortList item) {
    print('item selectState = ${item.selectState}');
    if (item.type != SPECIAL_TYPE) {
      widget.styleModel.teamViewSortList.forEach((listItem) {
        if (listItem.type != SPECIAL_TYPE && listItem.type != item.type) {
          listItem.selectState = 0;
        }
      });
      item.updateSelectState();
      setState(() {});
    } else {
      item.updateSelectState();
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: double.infinity,
      width: double.infinity,
      color: HexColor.fromHex('#FFFFFF'),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: widget.styleModel.teamViewSortList.map((item) {
          if (item.type != SPECIAL_TYPE) {
            return _getCustomWidget(
              item: item,
              text: item?.name,
              textColor: item.selectState == 0 ? item?.nameColor : item?.nameSelectedColor,
              icon: item.selectState == 0 ? item?.icon1 : item.selectState == 1 ? item.icon2 : item.selectState == 2 ? item.icon3 : item.icon1,
              isSelect: item.selectState != 0
            );
          } else {
            return _getCustomWidget(
              item: item,
              text: item.selectState == 0 ? item.args[0].name : item.selectState == 1 ? item.args[1].name : item.selectState == 2 ? item.args[2].name : item.args[0].name,
              textColor: item?.nameSelectedColor,
              icon: item?.icon1,
              isSelect: true
            );
          }
        }).toList(),
      ),
    );
  }

  Widget _getCustomWidget({TeamViewSortList item, String text, String textColor, String icon, bool isSelect = false}) {
    return GestureDetector(
      onTap: () => _onClick(item),
      child: Container(
        child: Row(
          children: <Widget>[
            Text(
              text ?? '',
              style: TextStyle(
                fontSize: 12,
                color: HexColor.fromHex(textColor),
                fontWeight: isSelect ? FontWeight.bold : FontWeight.w400
              ),
            ),
            const SizedBox(width: 3),
            CachedNetworkImage(
              imageUrl: icon ?? '',
              height: 10,
            )
          ],
        ),
      ),
    );
  }
}
