import 'dart:async';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:grpc/grpc.dart';
import '../../utils.dart';

class LoginController extends GetxController {
  final phone = "".obs;
  final password = "".obs;
  final codeRetryLeft = 0.seconds.obs;
  Timer? ticker;
  var isSignInEnable = true;
  final isAgreeContract = false.obs;


  @override
  void onInit() {
    super.onInit();




    ticker = Timer.periodic(1.seconds, (timer) {
      codeRetryLeft.value -= 1.seconds;
    });
  }

  void onGetCode() async {
    info("获取验证码");

    await tryApi(
        () async {
          // codeRetryLeft.value =
          //     await ApiService.to.getSmsSendLeftTime(phone.value);
          // if (codeRetryLeft.value.inSeconds > 0) {
          //   return;
          // }
          // await ApiService.to
          //     .authSendCodeToPhone(phone.value, pb.SmsType.Login);
          // codeRetryLeft.value =
          //     await ApiService.to.getSmsSendLeftTime(phone.value);
          info("获取验证码结束");
        },
        errTitle: '获取验证码失败',
        onError: (e) async{
          if (e.code == StatusCode.notFound) {
            Get.snackbar('用户未注册', "请先注册",
                isDismissible: true, duration: 3.seconds);
            return true;
          }

          return false;
        });
  }

  Future<void> onSignIn() async {
    // if (!isAgreeContract.value) {
    //   Get.snackbar('请先', '阅读并同意协议');
    //   return;
    // }

    if (!isSignInEnable) {
      return;
    }
    try {
      isSignInEnable = false;

      if (phone.value.isEmpty) {
        Get.snackbar('手机号错误', "请重新输入");
        return;
      }
      if (password.value.isEmpty) {
        Get.snackbar('验证码错误', "请重新输入");
        return;
      }

      final args = Get.arguments;

      await tryApi(
          () async {
            info("登录 ${phone.value} ${password.value}");
            // await ApiService.to.signIn(phone.value, password.value, '');
            //
            // if (Get.previousRoute == RouteName.signIn) {
            //   Get.offAllNamed(RouteName.home);
            // } else {
            //   Get.back(result: true, closeOverlays: true);
            // }
            final call = Get.arguments;
            info("登录成功");
            if(call is VoidCallback){
              call();
            }else{
              Get.back(result: true, closeOverlays: true);
            }

          },
          errTitle: '登录失败',
          onError: (e) async{
            switch (e.code) {
              case StatusCode.unauthenticated:
                Get.snackbar('登录失败', "验证码错误");
                return true;
              case StatusCode.notFound:
                Get.snackbar('用户未注册', "请先注册");
                return true;

              default:
                return false;
            }
          });
    } finally {
      isSignInEnable = true;
    }
  }

  @override
  void onClose() {
    ticker?.cancel();
  }
}

class LoginControllerMock extends LoginController {}
