import 'dart:convert';

import 'package:zhiying_base_widget/pages/about_us_page/model/about_us_model.dart';
import 'package:package_info/package_info.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class AboutUsRepository {
  /// 检查更新
  Future<AboutUsModel> checkUpdate() async {
    try {
      var result = await NetUtil.post('/api/v1/appcheck', method: NetMethod.POST);
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 返回父页面传进来的数据
  Future<AboutUsModel> fetchParentData(final Map<String, dynamic> data) async {
    try {
      if (!EmptyUtil.isEmpty(data) && !EmptyUtil.isEmpty(data['data'])) {
        PackageInfo packageInfo = await PackageInfo.fromPlatform();

        if (data['data'] is String) {
          AboutUsModel model = AboutUsModel.fromJson(jsonDecode(data['data']));
          if (!EmptyUtil.isEmpty(packageInfo) && !EmptyUtil.isEmpty(model)) {
            model.appVerison = packageInfo.version;
            return model;
          }
        }
        if (data['data'] is Map) {
          AboutUsModel model = AboutUsModel.fromJson(data['data']);
          if (!EmptyUtil.isEmpty(packageInfo) && !EmptyUtil.isEmpty(model)) {
            model.appVerison = packageInfo.version;
            return model;
          }
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 获取网络数据
  Future<AboutUsModel> fetchNetData(final Map<String, dynamic> data) async {
    try {
      String skip_identifier =
          !EmptyUtil.isEmpty(data) && data.containsKey('skip_identifier') && !EmptyUtil.isEmpty(data['skip_identifier']) ? data['skip_identifier'] : 'pub.flutter.about_us';

      var result = await NetUtil.post('/api/v1/mod/$skip_identifier', method: NetMethod.GET, cache: true);
      if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
        var modListData = result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list'][0]['data'];
        if (!EmptyUtil.isEmpty(modListData)) {
          PackageInfo packageInfo = await PackageInfo.fromPlatform();
          AboutUsModel model = AboutUsModel.fromJson(jsonDecode(modListData));
          if (!EmptyUtil.isEmpty(packageInfo) && !EmptyUtil.isEmpty(model)) {
            model.appVerison = packageInfo.version;
            return model;
          }
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 获取缓存数据
  Future<AboutUsModel> fetchCacheData(final Map<String, dynamic> data) async {
    try {
      String skip_identifier =
          !EmptyUtil.isEmpty(data) && data.containsKey('skip_identifier') && !EmptyUtil.isEmpty(data['skip_identifier']) ? data['skip_identifier'] : 'pub.flutter.about_us';
      var result = await NetUtil.getRequestCachedData('/api/v1/mod/$skip_identifier');
      if (!EmptyUtil.isEmpty(result)) {
        var modListData = result['mod_list'][0]['data'];
        if (!EmptyUtil.isEmpty(modListData)) {
          PackageInfo packageInfo = await PackageInfo.fromPlatform();
          AboutUsModel model = AboutUsModel.fromJson(jsonDecode(modListData));
          if (!EmptyUtil.isEmpty(packageInfo) && !EmptyUtil.isEmpty(model)) {
            model.appVerison = packageInfo.version;
            return model;
          }
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }
}
