import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:get/get.dart';
import 'package:naiyouwl/app/common/constants.dart';
import 'package:naiyouwl/app/controller/controllers.dart';
import 'package:naiyouwl/app/data/model/LoginMode.dart';
import 'package:naiyouwl/app/data/model/SysConfig.dart';

import '../../../common/LogHelper.dart';
import '../../../common/SharedPreferencesUtil.dart';
import '../../../data/model/LocalUser.dart';
import '../../../network/api_service.dart';
import '../../../routes/app_pages.dart';

class LoginController extends GetxController {

  final count = 0.obs;
  var isLoading = false.obs;
  var loginModes = LoginMode().obs;
  var localUsers = LocalUser().obs;
  var sysConfig = SysConfig().obs;
  var isReg = false.obs;
  Future<void> fetchLogin(String username, String password) async {
    try {
      if(username.isEmpty || password.isEmpty){
        controllers.global.handleApiError(Exception("账号密码不能为空"));
        return;
      }

      isLoading.value = true;
      loginModes.value = await ApiService().login(kLogin,data: {"email":username,"password":password});

      if(loginModes.value.accessToken != null){
        await SharedPreferencesUtil().setString("token", loginModes.value.accessToken.toString());
        var userModes = LocalUser(email: username,password: password,accessToken: loginModes.value.accessToken.toString());
        await SharedPreferencesUtil().setString("localUser", JsonMapper.serialize(userModes));
      }
      Get.offNamed(Routes.HOME,arguments: loginModes.value);

    } catch (e) {
      controllers.global.handleApiError(e);
    } finally {
      isLoading.value = false;
    }
  }


  Future<void> fetchRegLogin(String username, String password) async {
    try {
      if(username.isEmpty || password.isEmpty){
        controllers.global.handleApiError(Exception("账号密码不能为空"));
        return;
      }
      isLoading.value = true;
      loginModes.value = await ApiService().reg(KReg,data: {"email":username,"password":password});

      if(loginModes.value.accessToken != null){
        await SharedPreferencesUtil().setString("token", loginModes.value.accessToken.toString());
        var userModes = LocalUser(email: username,password: password,accessToken: loginModes.value.accessToken.toString());
        await SharedPreferencesUtil().setString("localUser", JsonMapper.serialize(userModes));
      }
      Get.offNamed(Routes.HOME,arguments: loginModes.value);

    } catch (e) {
      controllers.global.handleApiError(e);
    } finally {
      isLoading.value = false;
    }
  }

  Future<void> fetchLocalUser() async {
    try {
      String? data  = await SharedPreferencesUtil().getString("localUser");
      if(data  != null){
        localUsers.value = JsonMapper.deserialize<LocalUser>(data)!;
        LogHelper().d(localUsers.value.email);
      }
    } catch (e) {
      controllers.global.handleApiError(e);
    } finally {
      isLoading.value = false;
    }
  }

  Future<void> fetchSysConfig() async {
    try {
      String? data  = await SharedPreferencesUtil().getString("sysconfig");
      if(data  != null){
        sysConfig.value =JsonMapper.deserialize<SysConfig>(data)!;
      }

    } catch (e) {
      controllers.global.handleApiError(e);
    } finally {
    }
  }
  @override
  void onInit() {
    super.onInit();
    fetchLocalUser();
    fetchSysConfig();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void increment() => count.value++;

  String username() => localUsers.value.email.toString();
  String password() => localUsers.value.password.toString();
  String accToken() => localUsers.value.accessToken.toString();
}
