import 'package:flutter/rendering.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:keyboard/module/about/about_page.dart';
import 'package:keyboard/module/feedback/feedback_page.dart';

import '../../data/consts/error_code.dart';
import '../../data/repository/account_repository.dart';
import '../../resource/string.gen.dart';
import '../../utils/http_handler.dart';
import '../../utils/toast_util.dart';
import '../store/store_page.dart';

@injectable
class MineController extends BaseController {
  final AccountRepository accountRepository;

  MineController(this.accountRepository);

  bool get isLogin => accountRepository.isLogin.value;

  String? get phone => accountRepository.loginPhoneNum.value;

  String getUserName() {
    if (isLogin || phone != null && phone!.length > 4) {
      return '${StringName.mineAccountLoggedDesc}${phone!.substring(phone!.length - 4)}';
    } else {
      return StringName.mineAccountNoLogin;
    }
  }

  clickVip() {
    debugPrint('clickVip');
    StorePage.start();
  }

  clickOnlineCustomerService() {
    debugPrint('clickOnlineCustomerService');
    accountRepository
        .loginUserLogin("11223344551", "1122")
        .then((data) {
          Get.back();
          ToastUtil.show(StringName.loginSuccess);
        })
        .catchError((error) {
          if (error is LoginTooOftenException) {
            ToastUtil.show(StringName.loginTooOftenToast);
            return;
          }
          if (error is ServerErrorException) {
            if (error.code == ErrorCode.verificationCodeError) {
              ToastUtil.show(StringName.loginVerificationCodeErrorToast);
            } else {
              ToastUtil.show(error.message);
            }
          } else {
            ToastUtil.show(StringName.loginFailedToast);
          }
        });
  }

  clickTutorials() {
    debugPrint('clickTutorials');
  }

  clickPersonalProfile() {
    debugPrint('clickPersonalProfile');
    accountRepository.setUserInfo(name: "老铁",birthday: "2021-03-17",gender: 1,hobbies: ["a","b","c"],characters: ["测试"]);

  }

  clickFeedback() {
    debugPrint('clickFeedback');

    FeedbackPage.start();
  }

  clickAboutUs() {
    debugPrint('clickAboutUs');
    AboutPage.start();
  }
}
