import 'dart:io';
import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/consts/Constants.dart';
import 'package:electronic_assistant/data/consts/event_report_id.dart';
import 'package:electronic_assistant/handler/event_handler.dart';
import 'package:electronic_assistant/module/home/talk/view.dart';
import 'package:electronic_assistant/module/login/view.dart';
import 'package:electronic_assistant/module/main/controller.dart';
import 'package:electronic_assistant/module/record/view.dart';
import 'package:electronic_assistant/resource/string.gen.dart';
import 'package:electronic_assistant/router/app_pages.dart';
import 'package:electronic_assistant/utils/file_upload_check_helper.dart';
import 'package:electronic_assistant/utils/mmkv_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../data/repositories/account_repository.dart';
import '../../dialog/guide_dialog.dart';
import '../../resource/colors.gen.dart';
import '../../utils/toast_util.dart';
import '../audiopicker/view.dart';
import 'agenda/view.dart';

class HomePageController extends BaseController {
  get isLogin => accountRepository.isLogin.value;

  get loginTxt => accountRepository.isLogin.value
      ? accountRepository.getUserSubName(accountRepository.phone)
      : StringName.homeGoLogin.tr;

  final mainController = Get.find<MainController>();

  GlobalKey headGuideKey = GlobalKey();

  final ScrollController scrollController = ScrollController();

  late VoidCallback scrollCallback;

  final RxDouble _topBgOpacity = RxDouble(0);

  double get topBgOpacity => _topBgOpacity.value;

  final double maxScrollOffset = 100.h;

  //tab
  final tabList = [
    TabBean(StringName.homeTalkRecord.tr, TabType.talk, const HomeTalkView()),
    TabBean(StringName.talkSummaryTodoTitle.tr, TabType.agenda,
        const HomeAgendaView()),
  ];

  @override
  void onReady() {
    super.onReady();
    _checkMainGuide();
    scrollCallback = () {
      if (scrollController.offset <= maxScrollOffset) {
        _topBgOpacity.value = scrollController.offset / maxScrollOffset;
      }
    };
    scrollController.addListener(scrollCallback);
  }

  @override
  void onClose() {
    super.onClose();
    scrollController.removeListener(scrollCallback);
  }

  void _checkMainGuide() {
    if (!KVUtil.getBool(GuideConstants.mainGuide, true)) {
      return;
    }
    //新人引导
    mainGuide(headGuideKey, mainController.aiGuideKey, knowCallback: () {
      KVUtil.putBool(GuideConstants.mainGuide, false);
    });
  }

  void showLoginDrawer() {
    mainController.openDrawer();
  }

  void onPickerAudioFile() async {
    EventHandler.report(EventId.event_100030);
    if (!accountRepository.isLogin.value) {
      Get.toNamed(RoutePath.login);
      ToastUtil.showToast(StringName.errorCodeNoLogin.tr);
      return;
    }
    if (Platform.isAndroid) {
      _showAndroidFilePicker();
    } else {
      FileUploadCheckHelper.choicePlatformLocalFileAndCreateOrder();
    }
  }

  void onRecordClick() {
    RecordPage.start(fromType: RecordFromType.home);
  }

  void onLoginClick() {
    LoginPage.start(fromType: LoginFromType.mainLogin);
  }

  void _showAndroidFilePicker() {
    Get.bottomSheet(const AudioPickerPage(),
        isScrollControlled: true,
        barrierColor: ColorName.black55,
        backgroundColor: ColorName.transparent);
  }
}

class TabBean {
  String title;
  TabType type;
  Widget view;

  TabBean(this.title, this.type, this.view);
}

enum TabType { talk, agenda }
