import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:keyboard/data/api/response/user_info_response.dart';
import 'package:keyboard/data/bean/member_info.dart';
import 'package:keyboard/module/about/about_page.dart';
import 'package:keyboard/module/feedback/feedback_controller.dart';
import 'package:keyboard/module/feedback/feedback_page.dart';
import 'package:keyboard/module/user_info/user_info_page.dart';

import '../../data/consts/build_config.dart';
import '../../data/consts/web_url.dart';
import '../../data/repository/account_repository.dart';
import '../../dialog/login/login_dialog.dart';
import '../../plugins/keyboard_android_platform.dart';
import '../../resource/string.gen.dart';
import '../../utils/app_info_util.dart';
import '../../utils/date_util.dart';
import '../../utils/keyboard_tutorial_util.dart';
import '../../utils/toast_util.dart';
import '../browser/browser_page.dart';
import '../store/store_page.dart';
import '../user_profile/user_profile_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;

  UserInfoResponse? get userInfo => accountRepository.userInfo.value;

  MemberInfo? get memberInfo => accountRepository.memberStatusInfo.value;

  String getUserName() {
    if (!isLogin) return StringName.mineAccountNoLogin;

    final name = userInfo?.name;
    if (name != null && name.isNotEmpty) {
      return name;
    }

    if (phone != null && phone!.length > 4) {
      return '${StringName.mineAccountLoggedDesc}${phone!.substring(phone!.length - 4)}';
    }

    return StringName.mineAccountNoLogin;
  }

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

  longClickVip() {
    if (isLogin) {
      UserInfoPage.start();
    } else {
      LoginDialog.show();
    }
    // KeyboardAndroidPlatform.enableFloatingWindow(true);
    // KeyboardAndroidPlatform.openInputMethodSettings();
  }

  clickOnlineCustomerService() {
    final userInfo = accountRepository.userInfo.value;
    if (isLogin) {
      debugPrint('clickOnlineCustomerService');
      goToCustomerService();
    } else {
      ToastUtil.show('请先登录');
      LoginDialog.show();
      return;
    }
  }

  // 七鱼客服
  void goToCustomerService() {
    final userInfo = accountRepository.userInfo.value;
    if (userInfo == null) {
      ToastUtil.show('网络异常，请稍后再试');
      accountRepository.refreshUserInfo();
      return;
    }
    int appPlatform = 0;
    if (Platform.isAndroid) {
      appPlatform = 1;
    } else if (Platform.isIOS) {
      appPlatform = 2;
    }
    //拼接字符串
    String url =
        "${WebUrl.qiyuService}?ssid=${userInfo.ssid}&device_id=${userInfo.deviceId}&app_platform=$appPlatform&app_version=${appInfoUtil.appVersionName}&package_name=${appInfoUtil.packageName}&app_name=${appInfoUtil.appName}";
    BrowserPage.start(url);
  }

  clickUserCard() {
    if (isLogin) {
      UserInfoPage.start();
    } else {
      LoginPage.start();
      // LoginDialog.show();
    }
  }

  clickTutorials() {
    debugPrint('clickTutorials');
    KeyboardTutorialUtil.start();
  //   测试模isDebug式才生效
  //   if (BuildConfig.isDebug) {
  //     KeyboardAndroidPlatform.enableFloatingWindow(true);
  //     KeyboardAndroidPlatform.openInputMethodSettings();
  //   }
  }



  clickPersonalProfile() {
    debugPrint('clickPersonalProfile');
    UserProfilePage.start();
  }

  clickFeedback(FeedbackType type) {
    if (isLogin) {
      debugPrint('clickOnlineCustomerService');
      FeedbackPage.start(type);
    } else {
      ToastUtil.show('请先登录');
      LoginDialog.show();
      return;
    }
  }

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

  String getVipButtonDesc() {
    if (memberInfo?.isMember == true && isLogin) {
      if (memberInfo?.permanent == true && isLogin) {
        return StringName.vipLevel2Btn;
      }
      return StringName.vipLevel1Btn;
    }
    return StringName.vipLevel0Btn;
  }

  String getVipLevelDesc() {
    if (memberInfo?.isMember == true && isLogin) {
      if (memberInfo?.permanent == true && isLogin) {
        return StringName.vipLevel2Desc;
      }
      return "${StringName.vipLevel1Desc}${DateUtil.fromMillisecondsSinceEpoch('yyyy年MM月dd日', memberInfo?.endTimestamp ?? 0)}";
    }
    return StringName.vipLevel0Desc;
  }
}
