import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:news_getx/data/model/user.dart';
import 'package:news_getx/data/repository/user_repository.dart';
import 'package:news_getx/modules/widgets/toast.dart';
import 'package:news_getx/routes/app_pages.dart';
import 'package:news_getx/utils/security.dart';
import 'package:news_getx/utils/validator.dart';

class SignUpController extends GetxController {
  final UserRepository repository;

  // email的控制器
  final TextEditingController fullNameController = TextEditingController();

  // email的控制器
  final TextEditingController emailController = TextEditingController();

  // 密码的控制器
  final TextEditingController passwordController = TextEditingController();

  SignUpController({required this.repository});

  // 返回上一级
  handleNavPop() {
    Get.back();
  }

  handleRegister() async {
    if (!checkStringLength(fullNameController.text, 5)) {
      toastInfo(msg: '用户名不能小于5位');
      return;
    }
    if (!isEmail(emailController.text)) {
      toastInfo(msg: '请正确输入邮件');
      return;
    }
    if (!checkStringLength(passwordController.text, 6)) {
      toastInfo(msg: '密码不能小于6位');
      return;
    }

    UserRegisterRequest params = UserRegisterRequest(
      email: emailController.text, password: cryptoSha256(passwordController.text),
    );

    await repository.register(params);

    Get.back();
  }

  // 提示信息
  handleTip() {
    toastInfo(msg: '这是注册界面');
  }

  // 忘记密码
  handleFogotPassword() {
    toastInfo(msg: '忘记密码');
  }


  handleSignIn() {
    Get.toNamed(AppRoutes.Signin);
  }

  @override
  void dispose() {
    fullNameController.dispose();
    emailController.dispose();
    passwordController.dispose();
    super.dispose();
  }
}
