import 'dart:async';

import 'package:cpt_profile/modules/reset_password/reset_password_state.dart';
import 'package:flutter/material.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:router/ext/auto_router_extensions.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';

part 'reset_password_view_model.g.dart';

@riverpod
class ResetPasswordViewModel extends _$ResetPasswordViewModel {
  @override
  ResetPasswordState build() {
    final state = ResetPasswordState();
    initListener(state);
    ref.onDispose(() {
      onDispose(state);
    });
    return state;
  }

  /// 提交重置密码请求
  void submitResetPassword() {
    state = state.copyWith(codeErrorText: null, passwordErrorText: null, confirmPasswordErrorText: null);

    final FocusNode phoneFocusNode = state.formData['phone']!['focusNode'];
    final FocusNode codeFocusNode = state.formData['code']!['focusNode'];
    final FocusNode passwordFocusNode = state.formData['password']!['focusNode'];
    final FocusNode confirmPasswordFocusNode = state.formData['confirm_password']!['focusNode'];

    final TextEditingController phoneController = state.formData['phone']!['controller'];
    final TextEditingController codeController = state.formData['code']!['controller'];
    final TextEditingController passwordController = state.formData['password']!['controller'];
    final TextEditingController confirmPasswordController = state.formData['confirm_password']!['controller'];

    phoneFocusNode.unfocus();
    codeFocusNode.unfocus();
    passwordFocusNode.unfocus();
    confirmPasswordFocusNode.unfocus();

    final phone = phoneController.text;
    final code = codeController.text;
    final password = passwordController.text;
    final confirmPassword = confirmPasswordController.text;

    Log.d('当前待提交的 phone:$phone code:$code password:$password confirmPassword:$confirmPassword');

    if (Utils.isEmpty(phone)) {
      ToastEngine.show("Enter Mobile Phone");
      return;
    }

    if (Utils.isEmpty(code)) {
      state = state.copyWith(codeErrorText: "Verification Code cannot be empty!");
      return;
    }

    if (Utils.isEmpty(password)) {
      state = state.copyWith(passwordErrorText: "Password cannot be empty!");
      return;
    }

    if (Utils.isEmpty(confirmPassword)) {
      state = state.copyWith(confirmPasswordErrorText: "Confirm Password cannot be empty!");
      return;
    }

    if (confirmPassword != password) {
      state = state.copyWith(confirmPasswordErrorText: "Password mismatch, please check password");
      return;
    }

    //执行密码登录
    ToastEngine.show('准备执行请求发送验证码 phone:$phone code:$code password:$password confirmPassword:$confirmPassword');

    //页面返回
    appRouter.maybePop();
  }

  //切换隐藏显示密码
  void switchPwdVisibility() {
    state = state.copyWith(pwdVisibility: !state.pwdVisibility);
  }

  void switchConfirmPwdVisibility() {
    state = state.copyWith(confirmPwdVisibility: !state.confirmPwdVisibility);
  }

  void showVerifyCodedDialog() {
    _startCountDown();
  }

  Timer? countdownTimer;

  /// 开启倒计时
  void _startCountDown() {
    //60秒倒计时
    state = state.copyWith(isCounting: true, countdownTime: 60);

    //每秒的倒计时
    countdownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
      int time = state.countdownTime;
      Log.d('倒计时-->$time');
      if (time > 0) {
        time--;
        state = state.copyWith(isCounting: true, countdownTime: time);
      } else {
        state = state.copyWith(isCounting: false, countdownTime: 0);
        countdownTimer?.cancel(); // 取消计时器
      }
    });
  }

  //初始化监听
  void initListener(ResetPasswordState initState) {
    final FocusNode codeFocusNode = initState.formData['code']!['focusNode'];
    final FocusNode passwordFocusNode = initState.formData['password']!['focusNode'];
    final FocusNode confirmPasswordFocusNode = initState.formData['confirm_password']!['focusNode'];

    codeFocusNode.addListener(() {
      // 获取焦点的时候清空错误文本
      if (codeFocusNode.hasFocus) {
        state = state.copyWith(codeErrorText: null);
      }
    });

    passwordFocusNode.addListener(() {
      // 获取焦点的时候清空错误文本
      if (passwordFocusNode.hasFocus) {
        state = state.copyWith(passwordErrorText: null);
      }
    });

    confirmPasswordFocusNode.addListener(() {
      // 获取焦点的时候清空错误文本
      if (confirmPasswordFocusNode.hasFocus) {
        state = state.copyWith(confirmPasswordErrorText: null);
      }
    });
  }

  //销毁资源
  void onDispose(ResetPasswordState initState) {
    final FocusNode codeFocusNode = initState.formData['code']!['focusNode'];
    final FocusNode passwordFocusNode = initState.formData['password']!['focusNode'];
    final FocusNode confirmPasswordFocusNode = initState.formData['confirm_password']!['focusNode'];
    codeFocusNode.dispose();
    passwordFocusNode.dispose();
    confirmPasswordFocusNode.dispose();

    countdownTimer?.cancel();

    Log.d("ForgotVerifyViewModel 销毁 onDispose");
  }
}
