import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:keyboard/dialog/login/login_dialog.dart';
import 'package:keyboard/resource/string.gen.dart';
import 'package:keyboard/utils/toast_util.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import '../../../data/api/response/intimacy_analyze_config_response.dart';
import '../../../data/api/response/user_info_response.dart';
import '../../../data/bean/keyboard_info.dart';
import '../../../data/bean/member_info.dart';
import '../../../data/repository/account_repository.dart';
import '../../../data/repository/intimacy_analyze_repository.dart';
import '../../../data/repository/keyboard_repository.dart';
import '../../../utils/atmob_log.dart';
import '../../../utils/image_picker_util.dart';
import '../../../utils/intimacy_analyze_config_helper.dart';
import '../../profile/profile_page.dart';
import '../intimacy_analyse_upload/intimacy_analyse_upload_page.dart';

/// 亲密度分析-分析报告Tab-Controller
@injectable
class IntimacyAnalyseReportController extends BaseController {
  final String _tag = "IntimacyAnalyseReportController";

  /// 亲密度分析Repository
  IntimacyAnalyzeRepository intimacyAnalyzeRepository;

  /// 亲密度分析帮助类
  IntimacyAnalyzeConfigHelper intimacyAnalyzeConfigHelper;

  /// 键盘信息
  KeyboardRepository keyboardRepository;

  /// 用户信息Repository
  AccountRepository accountRepository;

  /// 亲密度配置
  Rxn<IntimacyAnalyzeConfigResponse> get intimacyAnalyzeConfig =>
      intimacyAnalyzeRepository.intimacyAnalyzeConfig;

  /// 当前键盘信息
  Rxn<KeyboardInfo> get currentKeyboardInfo =>
      keyboardRepository.chooseKeyboardInfo;

  /// 报告预览数据
  RxString reportPreviewData = "".obs;

  /// 是否登录
  bool get isLogin => accountRepository.isLogin.value;

  /// 当前用户信息
  Rxn<UserInfoResponse> get userInfo => accountRepository.userInfo;

  /// 当前用户的会员
  Rxn<MemberInfo> get memberInfo => accountRepository.memberStatusInfo;

  IntimacyAnalyseReportController(
    this.intimacyAnalyzeRepository,
    this.intimacyAnalyzeConfigHelper,
    this.keyboardRepository,
    this.accountRepository,
  );

  @override
  void onInit() {
    super.onInit();
    ever<IntimacyAnalyzeConfigResponse?>(intimacyAnalyzeConfig, (config) {
      updateIntimacyAnalyzeConfig(config);
    });
  }

  @override
  void onReady() {
    super.onReady();
    updateIntimacyAnalyzeConfig(intimacyAnalyzeConfig.value);
  }

  /// 更新亲密度配置
  void updateIntimacyAnalyzeConfig(IntimacyAnalyzeConfigResponse? config) {
    // 给页面的响应式变量赋值
    reportPreviewData.value = (config != null ? config.defaultContent : "")!;
  }

  /// 切换亲密档案
  void clickIntimacyArchivesSwitch() async {
    // 跳转到档案选择页，获取当前的键盘Id
    KeyboardInfo? keyboardInfo = await ProfilePage.start() as KeyboardInfo?;
    AtmobLog.d(_tag, "switchTaTest result：=> ${keyboardInfo?.toJson() ?? ""}");
  }

  /// 跳转去档案页
  void jump2ProfilePage() {
    // 如果用户未设置过档案，则跳转到档案选择页
    if (currentKeyboardInfo.value == null) {
      ToastUtil.show(StringName.noCreateIntimacyTaTip);
      ProfilePage.start();
      return;
    }
    ProfilePage.start();
  }

  /// 解锁
  void clickUnlockBtn(BuildContext context) async {
    // 如果用户未设置过档案，则跳转到档案选择页
    if (currentKeyboardInfo.value == null) {
      ToastUtil.show(StringName.noCreateIntimacyTaTip);
      ProfilePage.start();
      return;
    }
    // 跳转到图片选择，并返回选择的图片列表
    List<AssetEntity>? selectedAssetList = await ImagePickerUtil.pickImage(
      context,
      maxAssetsCount: intimacyAnalyzeConfigHelper.getMaxAssetsCount(),
    );
    // 跳转到亲密度分析上传页
    if (selectedAssetList?.isNotEmpty == true) {
      IntimacyAnalyseUploadPage.start(selectedAssetList!);
    }
  }
}
