import 'dart:io';

import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:injectable/injectable.dart';
import 'package:location/base/base_controller.dart';
import 'package:location/data/bean/member_status_info.dart';
import 'package:location/data/consts/error_code.dart';
import 'package:location/data/repositories/config_repository.dart';
import 'package:location/handler/error_handler.dart';
import 'package:location/module/feedback/feed_back_page.dart';
import 'package:location/module/login/login_page.dart';
import 'package:location/module/member/member_page.dart';
import 'package:location/module/urgent_contact/urgent_contact_page.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/http_handler.dart';

import '../../data/repositories/account_repository.dart';
import '../../data/repositories/member_repository.dart';
import '../../dialog/common_alert_dialog_impl.dart';
import '../../sdk/wechat/wechat_share_util.dart';
import '../../utils/app_info_util.dart';
import '../../utils/toast_util.dart';
import '../about/about_page.dart';
import '../browser/browser_view.dart';
import '../permission/permission_setting_page.dart';

@injectable
class MineController extends BaseController {
  final AccountRepository accountRepository;
  final ConfigRepository configRepository;
  final MemberRepository memberRepository;

  MineController(
      this.accountRepository, this.configRepository, this.memberRepository);

  bool get isLogin => accountRepository.isLogin.value;

  bool? get isOpenFreeMember => configRepository.isOpenFreeMember.value;

  MemberStatusInfo? get memberStatusInfo =>
      accountRepository.memberStatusInfo.value;

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

  void onBack() {
    Get.back();
  }

  String getUserName(String phone) {
    if (phone.length > 4) {
      phone = phone.substring(phone.length - 4);
    }
    return '${StringName.mineAccountLoggedDesc}$phone';
  }

  onShareClick() {
    WechatShareUtil.shareWebPage().catchError((error) {
      ToastUtil.show(error);
    });
  }

  onCustomerServiceClick() {
    String phone = AccountRepository.getInstance().loginPhoneNum.value ?? '';
    String url = "https://qiyu-kefu.atmob.com?"
        "ssid=$phone"
        "&device_id=$phone"
        "&app_platform=${Platform.isAndroid ? 1 : 2}"
        "&app_version=${appInfoUtil.appVersionName}"
        "&package_name=${appInfoUtil.packageName}"
        "&app_name=${StringName.appName}"
        "&phone=$phone";
    BrowserPage.start(url);
  }

  onPermissionSettingClick() {
    PermissionSettingPage.start();
  }

  onAccountFeedbackClick() {
    FeedBackPage.start();
  }

  onAboutClick() {
    AboutPage.start();
  }

  onLogoutAccountClick() {
    logoutAccountDialog(
        logoutCallback: () {
          accountRepository.userClear().then((_) {
            ToastUtil.show(StringName.accountLogoutSuccess);
            accountRepository.logout();
          }).catchError((error) {
            ErrorHandler.toastError(error);
          });
        },
        cancelCallback: () {});
  }

  onFunExitAccountClick() {
    exitAccountDialog(confirmOnTap: () {
      accountRepository.logout();
    });
  }

  onLoginClick() {
    if (isLogin) {
      return;
    }
    LoginPage.start();
  }

  onUrgentContactClick() {
    UrgentContactPage.start();
  }

  void onMemberTryOutClick() {
    memberRepository.memberTrial().then((freeMemberMinutes) {
      ToastUtil.show('已获得$freeMemberMinutes分钟会员试用');
    }).catchError((error) {
      if (error is ServerErrorException) {
        if (error.code == ErrorCode.getMemberFree) {
          ToastUtil.show(StringName.memberFreeCodeErrorToast);
        } else if (error.code == ErrorCode.isMember) {
          ToastUtil.show(StringName.memberFreeCodeIsmember);
        } else if (error.code == ErrorCode.noLoginError) {
          ToastUtil.show(StringName.accountNoLogin);
          LoginPage.start();
        } else {
          ToastUtil.show(error.message);
        }
      } else {
        ErrorHandler.toastError(error);
      }
    });
  }

  void onMemberCardClick() {
    MemberPage.start();
  }
}
