import 'dart:async';

import 'package:cpt_profile/modules/setting/setting_page.dart';
import 'package:flutter/material.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';

import 'change_mobile_state.dart';

part 'change_mobile_view_model.g.dart';

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

  /// 提交表单修改电话号码
  void submitChangeMobile() {
    state = state.copyWith(oldCodeErrorText: null, newCodeErrorText: null);

    final FocusNode oldMobileFocusNode = state.formData['old']!['focusNode'];
    final FocusNode oldCodeFocusNode = state.formData['old_code']!['focusNode'];
    final FocusNode newMobileFocusNode = state.formData['new']!['focusNode'];
    final FocusNode newCodeFocusNode = state.formData['new_code']!['focusNode'];

    final TextEditingController oldMobileController = state.formData['old']!['controller'];
    final TextEditingController oldCodeController = state.formData['old_code']!['controller'];
    final TextEditingController newMobileController = state.formData['new']!['controller'];
    final TextEditingController newCodeController = state.formData['new_code']!['controller'];

    oldMobileFocusNode.unfocus();
    oldCodeFocusNode.unfocus();
    newMobileFocusNode.unfocus();
    newCodeFocusNode.unfocus();

    final oldMobile = oldMobileController.text;
    final oldCode = oldCodeController.text;
    final newMobile = newMobileController.text;
    final newCode = newCodeController.text;

    Log.d('当前待提交的 oldMobile:$oldMobile oldCode:$oldCode newMobile:$newMobile newCode:$newCode');

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

    if (Utils.isEmpty(oldCode)) {
      state = state.copyWith(oldCodeErrorText: "Old mobile phone verification code cannot be empty!");
      return;
    }

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

    if (Utils.isEmpty(newCode)) {
      state = state.copyWith(newCodeErrorText: "New mobile phone verification code cannot be empty!");
      return;
    }

    //执行密码登录
    ToastEngine.show('准备执行请求发送验证码 oldMobile:$oldMobile oldCode:$oldCode newMobile:$newMobile newCode:$newCode');

    //返回设置页面
    SettingPage.startInstance();
  }

  //展示发送验证码弹窗
  showVerifyCodedDialog(bool isNewMobile) {
    if (isNewMobile) {
      _startNewCountDown();
    } else {
      _startOldCountDown();
    }
  }

  Timer? oldCountdownTimer;
  Timer? newCountdownTimer;

  /// 开启倒计时 - Old
  void _startOldCountDown() {
    //60秒倒计时
    state = state.copyWith(isOldCounting: true, oldCountdownTime: 60);

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

  /// 开启倒计时 - New
  void _startNewCountDown() {
    //60秒倒计时
    state = state.copyWith(isNewCounting: true, newCountdownTime: 60);

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

  //初始化监听
  void initListener(ChangeMobileState initState) {
    final FocusNode oldCodeFocusNode = initState.formData['old_code']!['focusNode'];
    final FocusNode newCodeFocusNode = initState.formData['new_code']!['focusNode'];

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

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

  //销毁资源
  void onDispose(ChangeMobileState initState) {
    final FocusNode oldCodeFocusNode = initState.formData['old_code']!['focusNode'];
    final FocusNode newCodeFocusNode = initState.formData['new_code']!['focusNode'];
    oldCodeFocusNode.dispose();
    newCodeFocusNode.dispose();

    oldCountdownTimer?.cancel();
    newCountdownTimer?.cancel();

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