import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/get_rx.dart';
import 'package:wallpaperx/common/components/easy_loading.dart';
import 'package:wallpaperx/common/components/view_state_widget.dart';
import 'package:wallpaperx/common/http/http_util.dart';
import 'package:wallpaperx/common/http/url.dart';
import 'package:wallpaperx/common/utils/log_print.dart';
import 'package:wallpaperx/entity/image_model.dart';
import 'package:wallpaperx/res/values/strings.dart';
import 'package:wallpaperx/routes/app_pages.dart';

class CategoryController extends GetxController {
  late ScrollController scrollController;
  late ViewState viewState;
  RxMap categoryData = {}.obs;
  RxString title = ''.obs;

  @override
  void onInit() {
    super.onInit();
    Map<String, dynamic> arguments = Get.arguments ?? {};
    title.value = arguments['title'] ?? appName;
    scrollController = ScrollController();
    getData();
  }

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

  void getData() {
    loading(show: true);
    HttpUtil.get(Url.byCategory, (callback) async {
      if (callback != null) {
        dynamic res = json.decode(callback);
        if (res["images"] != null) {
          categoryData.value = res["images"];
        }
      }
      dismiss(dismiss: true);
    }, params: {
      "category": title,
    });
  }

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

  /// 点击壁纸
  void onTapItem(List<ImageModel> wallpaperList, int index) {
    RxList rxList = RxList.from(wallpaperList);
    Get.toNamed(AppPages.wallpaperDet, arguments: {
      'position': index,
      'wallpaperList': rxList,
    });
  }
}
