import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hello_wallpaper/common/components/view_state_widget.dart';
import 'package:hello_wallpaper/models/wallpaper_model.dart';
import 'package:hello_wallpaper/modules/home/home_controller.dart';
import 'package:hello_wallpaper/routes/app_pages.dart';

class CatalogController extends GetxController {
  static CatalogController get to => Get.find<CatalogController>();
  late ScrollController scrollController;
  late ViewState viewState;
  late List<WallpaperModel> wallpaperModelList;

  @override
  void onInit() {
    super.onInit();
    scrollController = ScrollController();
    getData();
  }

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

  void getData() {
    wallpaperModelList = HomeController.to.wallpaperModelList;
    wallpaperModelList.shuffle();
    viewState = wallpaperModelList.isNotEmpty ? ViewState.normal : ViewState.empty;
    refresh();
  }

  /// 从分类中随机取5张
  List<WallpaperData> getRandomFiveList(WallpaperModel item) {
    if (item.data != null && item.data!.length >= 5) {
      var flatList = item.data!.map((e) => e).toList();
      return (flatList..shuffle()).take(5).toList();
    } else {
      return item.data == null ? <WallpaperData>[] : item.data!;
    }
  }

  /// 进入分类详情页面
  void onTapSingleCls(WallpaperModel wallpaperModel) {
    Get.toNamed(AppPages.clsDet, arguments: wallpaperModel);
  }

  /// 点击壁纸
  void onTapItem(String clsName, List<WallpaperData> wallpaperList, int index) {
    Get.toNamed(AppPages.wallpaperDet, arguments: {
      'position': index,
      'wallpaperList': wallpaperList,
    });
  }
}
