import 'dart:async';
import 'dart:convert';

import 'package:zhiying_base_widget/dialog/loading/loading.dart';
import 'package:zhiying_base_widget/pages/custom_page/event/reload_event.dart';
import 'package:zhiying_base_widget/pages/main_page/model/background_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class CustomPageRepository {
  Map<String, dynamic> data;

  CustomPageRepository({this.data});

  /// 初始化
  Future<CustomPageData> fetchInitData() async {
    CustomPageData customPageData=CustomPageData();
    try {
      String skipIdentifier =
          !EmptyUtil.isEmpty(data) && data.containsKey(GlobalConfig.SKIP_IDENTIFIER) && !EmptyUtil.isEmpty(data[GlobalConfig.SKIP_IDENTIFIER]) ? data[GlobalConfig.SKIP_IDENTIFIER] : null;
      // String skipIdentifier = 'pub.flutter.index.84';
      if (!EmptyUtil.isEmpty(skipIdentifier)) {
        var result = await NetUtil.post('/api/v1/mod/$skipIdentifier', method: NetMethod.GET, cache: true);
        if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
          List mobList = List.from(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list']);
          customPageData.modList=List();
          if (!EmptyUtil.isEmpty(mobList)) {
            for(var item in mobList){
              customPageData.modList.add(Map<String, dynamic>.from(item));
            }
            if(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA].containsKey('out_data')&&result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['out_data']!=''){
              try{
                var jsonData=json.decode(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['out_data']);
                print(jsonData);
                customPageData.backgroundModel=BackgroundModel.fromJson(jsonData);
              }catch(e,s){
                print(e);
                print(s);
              }

            }
            return customPageData;
          }
        }else{

          Timer(Duration(milliseconds: 1000), (){
            EventUtil.instance.fire(ReloadEvent());
          });

        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 缓存数据
  Future<CustomPageData> fetchCacheData() async {
    CustomPageData customPageData=CustomPageData();
    try {
      String skipIdentifier =
          !EmptyUtil.isEmpty(data) && data.containsKey(GlobalConfig.SKIP_IDENTIFIER) && !EmptyUtil.isEmpty(data[GlobalConfig.SKIP_IDENTIFIER]) ? data[GlobalConfig.SKIP_IDENTIFIER] : null;
      // String skipIdentifier = 'pub.flutter.index.84';
      if (!EmptyUtil.isEmpty(skipIdentifier)) {
        Map<String,dynamic> result = await NetUtil.getRequestCachedData('/api/v1/mod/$skipIdentifier');
        if (!EmptyUtil.isEmpty(result)) {
          List mobList = result['mod_list'];
          customPageData.modList=List();
          if (!EmptyUtil.isEmpty(mobList)) {
            for(var item in mobList){
              customPageData.modList.add(Map<String, dynamic>.from(item));
            }
            if(result.containsKey('out_data')&&result['out_data']!=''){
              var jsonData=json.decode(result['out_data']);
              print(jsonData);
              customPageData.backgroundModel=BackgroundModel.fromJson(jsonData);
            }
            return customPageData;
          }
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }
}


class CustomPageData{
  BackgroundModel backgroundModel;
  List<Map<String, dynamic>> modList;
}