import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/bbs_content.dart';

const brands = [
  '奥的斯',
  '富士达',
  '日立',
  '三菱',
  '东芝',
  '蒂森克虏伯',
  '西奥',
  '康力',
  '其他品牌',
];

const systems = [
  '默纳克',
  '新时达',
  '蓝光',
  '西子',
  '海普蒙特',
  '其他系统',
];

const skills = [
  '维保',
  '安装',
  '大修',
  '调试',
  'CAD',
];

const familiarities = [
  '电气',
  '机械',
  '调试',
  '其他方向',
];

const specialties = [
  '电气自动化',
  '机械工程',
  '其他专业',
];

const experiencedPositions = [
  '学徒',
  '技工',
  '组长',
  '区域主管',
  '中工',
  '大工',
  '项目经理',
  '其他岗位',
];

const traits = [
  '吃苦耐劳',
  '忠诚',
  '稳重',
  '坚守诚信',
  '乐于沟通',
  '善于学习',
  '勤奋务实',
  '适应能力强',
];

class LabelModel {
  String title;
  bool selected;

  LabelModel({this.title, this.selected});
}

class LabelCollection extends StatefulWidget {
  final List<String> selectedLabels;

  LabelCollection({this.selectedLabels});

  @override
  State<StatefulWidget> createState() => LabelCollectionState();
}

class LabelCollectionState extends State<LabelCollection> {
  List<LabelModel> brandModelList = [];
  List<LabelModel> systemModelList = [];
  List<LabelModel> skillModelList = [];
  List<LabelModel> familiarityModelList = [];
  List<LabelModel> specialtyModelList = [];
  List<LabelModel> positionModelList = [];
  List<LabelModel> traitModelList = [];

  @override
  initState() {
    super.initState();

    dealWithModels(brandModelList, brands);
    dealWithModels(systemModelList, systems);
    dealWithModels(skillModelList, skills);
    dealWithModels(familiarityModelList, familiarities);
    dealWithModels(specialtyModelList, specialties);
    dealWithModels(positionModelList, experiencedPositions);
    dealWithModels(traitModelList, traits);
  }

  void dealWithModels(List<LabelModel> modelList, List<String> strList) {
    modelList.addAll(strList.map((e) =>
        LabelModel(title: e, selected: widget.selectedLabels.contains(e))));
  }

  void feedSelectedLabels(List<LabelModel> modelList) {
    for (var model in modelList) {
      if (model.selected) {
        widget.selectedLabels.add(model.title);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: '标签',
        onBack: () {
          widget.selectedLabels.clear();

          feedSelectedLabels(brandModelList);
          feedSelectedLabels(systemModelList);
          feedSelectedLabels(skillModelList);
          feedSelectedLabels(familiarityModelList);
          feedSelectedLabels(specialtyModelList);
          feedSelectedLabels(positionModelList);
          feedSelectedLabels(traitModelList);

          Navigator.pop(context);
        },
      ),
      body: ListView(
        children: [
          CommonSectionHeader(
            title: '品牌类',
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
            child: Wrap(
              spacing: 5,
              runSpacing: 5,
              children: brandModelList.map((e) => labelButton(e)).toList(),
            ),
          ),
          CommonSectionHeader(
            title: '系统类',
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
            child: Wrap(
              spacing: 5,
              runSpacing: 5,
              children: systemModelList.map((e) => labelButton(e)).toList(),
            ),
          ),
          CommonSectionHeader(
            title: '技能类',
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
            child: Wrap(
              spacing: 5,
              runSpacing: 5,
              children: skillModelList.map((e) => labelButton(e)).toList(),
            ),
          ),
          CommonSectionHeader(
            title: '擅长方向',
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
            child: Wrap(
              spacing: 5,
              runSpacing: 5,
              children:
                  familiarityModelList.map((e) => labelButton(e)).toList(),
            ),
          ),
          CommonSectionHeader(
            title: '专业方向',
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
            child: Wrap(
              spacing: 5,
              runSpacing: 5,
              children: specialtyModelList.map((e) => labelButton(e)).toList(),
            ),
          ),
          CommonSectionHeader(
            title: '曾任岗位',
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
            child: Wrap(
              spacing: 5,
              runSpacing: 5,
              children: positionModelList.map((e) => labelButton(e)).toList(),
            ),
          ),
          CommonSectionHeader(
            title: '性格品质',
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
            child: Wrap(
              spacing: 5,
              runSpacing: 5,
              children: traitModelList.map((e) => labelButton(e)).toList(),
            ),
          ),
        ],
      ),
    );
  }

  Widget labelButton(LabelModel model) {
    return GestureDetector(
        onTap: () {
          setState(() {
            model.selected = !model.selected;
          });
        },
        child: Container(
          padding: EdgeInsets.all(10),
          decoration: BoxDecoration(
            color: model.selected ? Color(0xffECF2FF) : Color(0xffF4F8FA),
            borderRadius: BorderRadius.circular(20),
          ),
          child: Text(
            model.title,
            style: TextStyle(
              color: model.selected ? Color(0xff5589FF) : Color(0xff666666),
              fontSize: 13,
            ),
          ),
        ));
  }
}
