import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallpaperx/applovin_max/applovin_manage.dart';
import 'package:wallpaperx/common/components/view_state_widget.dart';
import 'package:wallpaperx/common/utils/shared_util.dart';
import 'package:wallpaperx/config/app_tracking_transparency_manager.dart';
import 'package:wallpaperx/entity/userinfo_model.dart';
import 'package:wallpaperx/generated/assets.dart';
import 'package:wallpaperx/page/discover/discover_view.dart';
import 'package:wallpaperx/page/library/library_view.dart';
import 'package:wallpaperx/page/settings/settings_view.dart';
import 'package:wallpaperx/res/values/strings.dart';

import '../../routes/app_pages.dart';

class HomeController extends GetxController with WidgetsBindingObserver {
  static HomeController get to => Get.find<HomeController>();
  final pages = [
    PageItem(
        'Discover',
        appName,
        [Assets.imagesDiscoverSelected, Assets.imagesDiscoverSelected],
        const DiscoverView()),
    PageItem(
        'Library',
        appName,
        [Assets.imagesFavoriteSelected, Assets.imagesFavoriteSelected],
        const LibraryView()),
    PageItem(
        'Setting',
        appName,
        [Assets.imagesSettingSelected, Assets.imagesSettingSelected],
        const SettingsView()),
  ];
  late PageController pageController;
  var currentIndex = 0.obs;
  var viewState = ViewState.loading;
  UserinfoModel userinfo = UserinfoModel();

  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();

  List<String> categoryList = [
    "Anime and Game Design",
    "Brand and Visual Design",
    "Architecture and Space Design",
    "Product and Industrial Design",
    "Outdoor and Nature",
    "Styles and Themes",
    "Characters and Fashion",
    "Photography",
  ];

  List<String> categoryImgList = [
    "assets/category/category1.png",
    "assets/category/category2.png",
    "assets/category/category3.png",
    "assets/category/category4.png",
    "assets/category/category5.png",
    "assets/category/category6.png",
    "assets/category/category7.png",
    "assets/category/category8.png",
  ];

  @override
  void onInit() {
    super.onInit();
    getUserInfo();
    WidgetsBinding.instance.addObserver(this);
    pageController = PageController(initialPage: currentIndex.value);
    AppTrackingTransparencyManager().requestATT();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    WidgetsBinding.instance.removeObserver(this);
    pageController.dispose();
    super.onClose();
  }

  @override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
    switch (state) {
      case AppLifecycleState.resumed:
        // await ApplovinManage().showAdIfReady(ApplovinManage().adUnitId1);
        break;
      case AppLifecycleState.inactive:
      case AppLifecycleState.hidden:
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
        break;
    }
  }

  void openDrawer() {
    scaffoldKey.currentState?.openDrawer();
  }

  /// PageView页面改变回调
  void onPageChanged(int index) {
    currentIndex.value = index;
  }

  /// 点击BottomNavigationBar
  void onTapNavigationBar(int index) {
    pageController.jumpToPage(index);
    // ApplovinManage().showAdIfReady(ApplovinManage().adUnitId1);
  }

  /// 获取用户信息
  void getUserInfo() {
    var loginAccount = UPCache.getInstance().get("loginAccount");
    var map = UPCache.getInstance().getJson(loginAccount??"");
    if (map != null) {
      userinfo = UserinfoModel.fromJson(map);
    }
  }

  /// 跳转分类
  void onTapToCategory(title) {
    scaffoldKey.currentState?.openEndDrawer();
    Get.toNamed(AppPages.category, arguments: {
      'title': title,
    });
  }
}

class PageItem {
  late final String label;
  late final String appName;
  late final List<String> icons;
  late final StatelessWidget widget;

  PageItem(this.label, this.appName, this.icons, this.widget);
}
