import 'dart:async';

import 'package:dartin/dartin.dart';
import 'package:flutter/material.dart';
import 'package:sport/provider/login_info_model.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/button_primary.dart';
import 'package:sport/widgets/code_input.dart';
import 'package:sport/widgets/dialog/request_dialog.dart';
import 'package:sport/widgets/loading.dart';

typedef CaptchaCall = Future Function(String captcha);

class AuthCodeVerifyPage extends StatefulWidget {
  final String _phone;
  final CaptchaCall _action;

  AuthCodeVerifyPage(this._phone, this._action);

  @override
  State<StatefulWidget> createState() {
    return _PageState();
  }
}

class _PageState extends State<AuthCodeVerifyPage> {
  Timer _timer;
  int _seconds;
  bool _isShowAuthCode;
  LoginInfoModel _loginInfoModel = inject();
  bool _isLoading;

  @override
  void initState() {
    super.initState();
    _seconds = 60;
    startTimer();
    _isShowAuthCode = false;
    _isLoading = false;
  }

  void cancelTimer() {
    if (_timer != null) {
      _timer.cancel();
      _timer = null;
    }
  }

  void startTimer() {
    cancelTimer();
    _seconds = 60;
    const period = const Duration(seconds: 1);
    _timer = Timer.periodic(period, (timer) {
      //更新界面
      setState(() {
        //秒数减一，因为一秒回调一次
        _seconds--;
        if (_seconds == 0) {
          //倒计时秒数为0，取消定时器
          cancelTimer();
          setState(() {
            _isShowAuthCode = true;
          });
        }
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    cancelTimer();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        leading: buildBackButton(context),
      ),
      body: Container(
        color: Colors.white,
        width: double.infinity,
        child: Column(
          children: <Widget>[
            // head
            Padding(
              padding: EdgeInsets.only(bottom: 20.0, top: 70.0),
              child: Text("输入验证码", style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1), fontSize: 25.0, fontWeight: FontWeight.bold)),
            ),
            Padding(
              padding: EdgeInsets.only(bottom: 16.0),
              child: Text(
                "验证码已发送至 +${widget._phone}",
                style: TextStyle(color: Color.fromRGBO(102, 102, 102, 1), fontSize: 14.0),
              ),
            ),
            Stack(
              children: <Widget>[
                _isLoading ? FadeingCircleLoading(context) : Container(),
                Padding(
                    padding: EdgeInsets.symmetric(vertical: 16.0, horizontal: 70.0),
                    // onSumbit 是输入完的回调 判断一手
                    child: LcfarmCodeInput(
                      codeLength: 4,
                      autoFocus: true,
                      onSubmit: (value) async {
                        var result = await request(context, () async {
                          return await widget._action.call(value);
                        });
                        Navigator.of(context).pop(result);
                      },
                    )),
              ],
            ),
            SizedBox(height: 24.0,),
            PrimaryButton(
              width: 300,
              height: 44.0,content: _isShowAuthCode == false ? "$_seconds秒后重新获取验证码" : "重新获取验证码",callback: _isShowAuthCode == false ? null : () {
              if (_isShowAuthCode) {
//                  print("重新获取验证码");
                _loginInfoModel.getCaptcha(widget._phone).then((value) {
                  setState(() {
                    _isShowAuthCode = false;
                    startTimer();
                  });
                });
              }
            },),
          ],
        ),
      ),
    );
  }
}
