import 'package:cpt_auth/modules/sing_up_verify/sign_up_verify_page.dart';
import 'package:flutter/material.dart';
import 'package:plugin_basic/dialog/verify_code_dialog.dart';
import 'package:plugin_platform/engine/dialog/dialog_engine.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 'sign_up_state.dart';

part 'sign_up_view_model.g.dart';

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

  //提交注册表单
  void submitSignUp() {
    state = state.copyWith(
      firstNameErrorText: null,
      lastNameErrorText: null,
      emailErrorText: null,
      phoneErrorText: null,
      passwordErrorText: null,
      confirmPasswordErrorText: null,
    );

    final FocusNode firstNameFocusNode = state.formData['first_name']!['focusNode'];
    final FocusNode lastNameFocusNode = state.formData['last_name']!['focusNode'];
    final FocusNode emailFocusNode = state.formData['email']!['focusNode'];
    final FocusNode phoneFocusNode = state.formData['phone']!['focusNode'];
    final FocusNode passwordFocusNode = state.formData['password']!['focusNode'];
    final FocusNode confirmPasswordFocusNode = state.formData['confirm_password']!['focusNode'];

    firstNameFocusNode.unfocus();
    lastNameFocusNode.unfocus();
    emailFocusNode.unfocus();
    phoneFocusNode.unfocus();
    passwordFocusNode.unfocus();
    confirmPasswordFocusNode.unfocus();

    final TextEditingController firstNameController = state.formData['first_name']!['controller'];
    final TextEditingController lastNameController = state.formData['last_name']!['controller'];
    final TextEditingController emailController = state.formData['email']!['controller'];
    final TextEditingController phoneController = state.formData['phone']!['controller'];
    final TextEditingController passwordController = state.formData['password']!['controller'];
    final TextEditingController confirmPasswordController = state.formData['confirm_password']!['controller'];

    final firstName = firstNameController.text;
    final lastName = lastNameController.text;
    final email = emailController.text;
    final phone = phoneController.text;
    final password = passwordController.text;
    final confirmPassword = confirmPasswordController.text;

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

    if (Utils.isEmpty(firstName)) {
      state = state.copyWith(firstNameErrorText: "First Name cannot be empty!");
      return;
    }

    if (Utils.isEmpty(lastName)) {
      state = state.copyWith(lastNameErrorText: "Last Name cannot be empty!");
      return;
    }

    if (Utils.isEmpty(email)) {
      state = state.copyWith(emailErrorText: "Email cannot be empty!");
      return;
    }

    if (Utils.isEmpty(phone)) {
      state = state.copyWith(phoneErrorText: "Email 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;
    }


    //短信验证码之前的图片验证码弹窗
    DialogEngine.show(
      onDismiss: () {},
      widget: VerifyCodeDialog(
        confirmAction: (key, code) {
          //去新页面发送短信验证码并校验和注册用户
          SignUpVerifyPage.startInstance(
            firstName: firstName,
            lastName: lastName,
            email: email,
            phone: "${state.countryCode}$phone",
            password: password,
            confirmPassword: confirmPassword,
            verifyKey: key,
            verifyCode: code,
          );
        },
      ),
    );

  }

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

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

  //切换同意按钮
  void switchAgreeTerms() {
    state = state.copyWith(isAgreeTerms: !state.isAgreeTerms);
  }

  void gotoTermsPage() {
    ToastEngine.show("去协议页面");
  }

  //初始化监听
  void initListener(SignUpState initState) {
    final FocusNode firstNameFocusNode = initState.formData['first_name']!['focusNode'];
    final FocusNode lastNameFocusNode = initState.formData['last_name']!['focusNode'];
    final FocusNode emailFocusNode = initState.formData['email']!['focusNode'];
    final FocusNode phoneFocusNode = initState.formData['phone']!['focusNode'];
    final FocusNode passwordFocusNode = initState.formData['password']!['focusNode'];
    final FocusNode confirmPasswordFocusNode = initState.formData['confirm_password']!['focusNode'];

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

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

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

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

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

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

  //销毁资源
  void onDispose(SignUpState initState) {
    final FocusNode firstNameFocusNode = initState.formData['first_name']!['focusNode'];
    final FocusNode lastNameFocusNode = initState.formData['last_name']!['focusNode'];
    final FocusNode emailFocusNode = initState.formData['email']!['focusNode'];
    final FocusNode phoneFocusNode = initState.formData['phone']!['focusNode'];
    final FocusNode passwordFocusNode = initState.formData['password']!['focusNode'];
    final FocusNode confirmPasswordFocusNode = initState.formData['confirm_password']!['focusNode'];
    firstNameFocusNode.dispose();
    lastNameFocusNode.dispose();
    emailFocusNode.dispose();
    phoneFocusNode.dispose();
    passwordFocusNode.dispose();
    confirmPasswordFocusNode.dispose();

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

  //保存当前选择的国家区号
  void saveCountryCode(String? countryCode) {
    state = state.copyWith(countryCode: countryCode);
    Log.d("saveCountryCode:${state.countryCode}");
  }

}
