import 'package:flutter/material.dart';
import 'package:get/get.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/config/applovin.dart';
import 'package:wallpaperx/entity/userinfo_model.dart';
import 'package:wallpaperx/generated/assets.dart';
import 'package:wallpaperx/page/custom/custom_view.dart';
import 'package:wallpaperx/page/library/library_view.dart';
import 'package:wallpaperx/page/recommend/recommend_view.dart';
import 'package:wallpaperx/page/settings/settings_view.dart';
import 'package:wallpaperx/routes/app_pages.dart';

class HomeController extends GetxController with WidgetsBindingObserver {
  static HomeController get to => Get.find<HomeController>();
  final pages = [
    PageItem(Assets.imagesRecommendSelected, const RecommendView()),
    PageItem(Assets.imagesCollectionSelected, const LibraryView()),
    PageItem(Assets.imagesCustomSelected,  const CustomView()),
    PageItem(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> category = [
    "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> categoryImages = [
    "assets/category/category_img_1.png",
    "assets/category/category_img_2.png",
    "assets/category/category_img_3.png",
    "assets/category/category_img_4.png",
    "assets/category/category_img_5.png",
    "assets/category/category_img_6.png",
    "assets/category/category_img_7.png",
    "assets/category/category_img_8.png",
  ];

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

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

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

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

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

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

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

  /// 跳转分类
  void toCategory(title, {String? jumpType}) {
    scaffoldKey.currentState?.openEndDrawer();
    ApplovinUtil().showAdIfReady().then((e) {
      Get.toNamed(AppPages.category, arguments: {
        'title': title,
      });
    });
  }
}

class PageItem {
  late final String icons;
  late final StatelessWidget widget;

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