import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_comm/util/extension/color.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class SelectDateYMDialog extends StatefulWidget {
  int currentYear;

  int currentMonth;

  SelectDateYMDialog({Key key, this.currentYear = 0, this.currentMonth = 0})
      : super(key: key) {
    var date = DateTime.now();
    if (currentYear == 0) {
      currentYear = date.year;
    }
    if (currentMonth == 0) {
      currentMonth = date.month;
    }
  }

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

class _SelectDateYMDialogState extends State<SelectDateYMDialog>
    with TickerProviderStateMixin {
  List<int> yearDate = List();
  List<int> monthDate = List();

  int selectYearIndex = 0;

  int selectMonthIndex = 0;

  ScrollController _yearScrollController;

  ScrollController _monthScrollController;

  double _containerHeight = 0;
  AnimationController _animationController;
  Animation _animation;

  @override
  void initState() {
    _monthScrollController = FixedExtentScrollController();
    for (int index = 0; index < 200; index++) {
      yearDate.add(index + 1900);
      if (index + 1900 == widget.currentYear) {
        selectYearIndex = index;
      }
    }

    for (int index = 0; index < 12; index++) {
      monthDate.add(index + 1);
      if (index + 1 == widget.currentMonth) {
        selectMonthIndex = index;
      }
    }
    _yearScrollController =
        FixedExtentScrollController(initialItem: selectYearIndex);

    _monthScrollController =
        FixedExtentScrollController(initialItem: selectMonthIndex);

    _animationController = new AnimationController(
        duration: const Duration(milliseconds: 500), vsync: this);
    final CurvedAnimation curve =
        new CurvedAnimation(parent: _animationController, curve: Curves.ease);
    _animation = new Tween(begin: 0.0, end: 250.0).animate(curve);
    _animation.addListener(() {
      setState(() {});
    });
    _animationController.forward();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          Container(
            height: 56,
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(20),
                    topRight: Radius.circular(20))),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                GestureDetector(
                  child: Padding(
                    padding: const EdgeInsets.only(
                        left: 16, top: 8, bottom: 8, right: 16),
                    child: Text(
                      "取消",
                      style: TextStyle(
                          color: HexColor.fromHex("#FF999999"),
                          fontSize: 35.sp),
                    ),
                  ),
                  onTap: () {
                    Navigator.pop(context);
                  },
                ),
                Text("选择日期"),
                GestureDetector(
                  child: Padding(
                    padding: const EdgeInsets.only(
                        right: 16, left: 16, bottom: 8, top: 8),
                    child: Text(
                      "确定",
                      style: TextStyle(
                          color: HexColor.fromHex("#FFFF4242"),
                          fontSize: 35.sp),
                    ),
                  ),
                  onTap: () {
                    Navigator.of(context).pop({
                      "year": yearDate[selectYearIndex].toString(),
                      "month": monthDate[selectMonthIndex].toString()
                    });
                  },
                )
              ],
            ),
          ),
          Container(
            height: _animation.value,
            color: Colors.white,
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: CupertinoPicker(
                          scrollController: _yearScrollController,
                          backgroundColor: Colors.white,
                          itemExtent: 40,
                          onSelectedItemChanged: (index) {
                            selectYearIndex = index;
                            setState(() {});
                          },
                          children: _buildItem(context))),
                  flex: 1,
                ),
                Expanded(
                  child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: CupertinoPicker(
                        scrollController: _monthScrollController,
                        backgroundColor: Colors.white,
                        itemExtent: 40,
                        onSelectedItemChanged: (index) {
                          selectMonthIndex = index;
                          setState(() {});
                        },
                        children: _buildItem2(context),
                      )),
                  flex: 1,
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  _buildItem(BuildContext context) {
    List<Widget> listWidget = List();

    for (int index = 0; index < yearDate.length; index++) {
      listWidget.add(Container(
        height: 40,
        child: Center(
            child: Text(
          yearDate[index].toString() + "",
          style: TextStyle(
              color:
                  index == selectYearIndex ? Colors.redAccent : Colors.black26),
        )),
      ));
    }
    return listWidget;
  }

  _buildItem2(BuildContext context) {
    List<Widget> listWidget = List();

    for (int index = 0; index < monthDate.length; index++) {
      listWidget.add(Container(
          height: 40,
          child: Center(
              child: Text(monthDate[index].toString() + "月",
                  style: TextStyle(
                      color: index == selectMonthIndex
                          ? Colors.redAccent
                          : Colors.black26)))));
    }
    return listWidget;
  }
}
