import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/utils/CustomUI.dart';
import 'package:flutter/material.dart';

import 'package:oktoast/oktoast.dart';

//自定义 地址选择
class SelectPage extends StatefulWidget {
  @required
  final String title;
  @required
  final dataId;
  final str;
  final callback;
  final provinces;
  final cities;
  final isSingle;
  final int mostNum;
  SelectPage(
      {Key key,
      this.str,
      this.title,
      this.dataId,
      this.callback,
      this.provinces,
      this.cities,
      this.isSingle,
      this.mostNum})
      : super(key: key);
  _SelectPageState createState() => _SelectPageState();
}

class _SelectPageState extends State<SelectPage> {
  String leftSelection;
  Set<String> tempRankId = new Set();
  @override
  void initState() {
    super.initState();
    leftSelection = widget.provinces.keys.first;
    tempRankId = widget.dataId.toSet();
  }

  @override
  void dispose() {
    tempRankId.clear();

    super.dispose();
  }

  List<Widget> _buildLeftSelection() {
    List<Widget> result = [];
    widget.provinces.forEach((k, v) => result.add(_buildItem(k, v, true)));
    return result;
  }

  List<Widget> _buildRightSelection() {
    List<Widget> result = [];
    widget.cities[leftSelection].forEach(
        (k, v) => result.add(_buildItem('$leftSelection-$k', v, false)));
    return result;
  }

  Widget _buildItem(key, value, isLeft) {
    return InkWell(
        onTap: () {
          if (isLeft) {
            setState(() {
              leftSelection = key;
            });
          } else {
            setState(() {
              // //常驻城市不能跨国家选中
              if (!widget.isSingle &&
                  tempRankId.length > 0 &&
                  tempRankId.first.split('-')[0] != leftSelection) {
                showToast('常驻城市不能跨国家');
                return;
              }
              if (tempRankId.contains(key)) {
                if (!widget.isSingle) tempRankId.remove(key);
              } else {
                if (tempRankId.length >= widget.mostNum) {
                  showToast(I18n.of(context)
                      .most
                      .replaceFirst('/s1', widget.mostNum.toString()));
                  return;
                }
                tempRankId.add(key);
              }
            });
            //单选直接关闭
            if (widget.isSingle) {
              if (widget.dataId.length != 0 && tempRankId.length >= 2) {
                tempRankId.remove(widget.dataId.first);
              }
              widget.callback(tempRankId);
            }
          }
        },
        highlightColor: Colors.transparent,
        radius: 0.0,
        child: new Container(
          height: 50,
          child: new Align(
            alignment: Alignment.center,
            child: new Text(
              value,
              textScaleFactor: 1.0,
              textAlign: TextAlign.center,
              style: isLeft
                  ? TextStyle(
                      color: leftSelection == key
                          ? Colors.white
                          : Constants.BlackTextColor)
                  : TextStyle(
                      color: tempRankId.contains(key)
                          ? Constants.BlueTextColor
                          : Constants.BlackTextColor),
            ),
          ),
          decoration: BoxDecoration(
            color:
                leftSelection == key ? Constants.BlueTextColor : Colors.white,
            border: Border(top: BorderSide(color: Colors.grey, width: 0.5)),
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    Widget appBar = new AppBar(
      title: new Text(widget.title, textScaleFactor: 1.0),
      elevation: 0,
      leading: CustomUI.buildCustomLeading(context),
      actions: <Widget>[
        new Container(
          alignment: Alignment.center,
          child: new InkWell(
            child: new Padding(
              padding:
                  EdgeInsets.only(right: 15, left: 15, top: 10, bottom: 10),
              child: new Text(I18n.of(context).determine,
                  textScaleFactor: 1.0, style: Constants.AppBarActionTextStyle),
            ),
            onTap: () {
              widget.callback(tempRankId);
            },
          ),
        )
      ],
      centerTitle: true,
    );
    return Scaffold(
      body: SafeArea(
          child: Center(
        child: Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          child: Row(
            children: <Widget>[
              Container(
                  width: MediaQuery.of(context).size.width * 0.5,
                  child: ListView(
                    children: _buildLeftSelection(),
                  ),
                  decoration: BoxDecoration(
                    border:
                        Border(right: BorderSide(color: Colors.grey, width: 1)),
                  )),
              Container(
                width: MediaQuery.of(context).size.width * 0.5,
                child: ListView(
                  children: _buildRightSelection(),
                ),
              ),
            ],
          ),
        ),
      )),
      appBar: appBar,
    );
  }
}
