import 'dart:convert';

import 'package:zhiying_comm/util/shared_prefe_util.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

// 应用配置信息

class AppConfigModel {
  // 应用配置的key
  AppConfigKeyModel keys;

  //  引导页
  AppConfigGuideModel guideImage;

  ///新引导页参数
  GuideData guideData;

  static String appStartDelay = "0.5";

  static AppConfigModel _config;

  AppConfigModel({this.keys, this.guideImage});

  static AppConfigModel getConfig() => _config;

  static Future<AppConfigModel> init({bool isGetCache = false}) async {
    try {
      String cacheData = await SharedPreferencesUtil.getStringValue(GlobalConfig.GUIDE, defaultVal: '1');
      if (isGetCache && cacheData != '1') {
        _config = AppConfigModel.fromJson(Map<String, dynamic>.from(json.decode(cacheData)));
        Logger.debug('基础设置初始化');
        NetUtil.request('/api/v1/app/guide', onSuccess: (data) {
          Logger.log(json.encode(data));
          var cacheString = json.encode(data);
          SharedPreferencesUtil.setNetCacheResult(GlobalConfig.GUIDE, cacheString);
        });
        return _config;
      } else {
        Map result = await NetUtil.post('/api/v1/app/guide', method: NetMethod.GET);
        var data = result['data'];
        _config = AppConfigModel.fromJson(Map<String, dynamic>.from(data));
        Logger.debug('基础设置初始化');
        SharedPreferencesUtil.setNetCacheResult(GlobalConfig.GUIDE, json.encode(data));
        return _config;
      }
    } catch (err) {
      Logger.error(err);

      return null;
    }
  }

  AppConfigModel.fromJson(Map<String, dynamic> json) {
    keys = json['keys'] != null ? new AppConfigKeyModel.fromJson(json['keys']) : null;
    guideImage = json['guide_image'] != null ? new AppConfigGuideModel.fromJson(json['guide_image']) : null;
    guideData = json['guide_data'] != null ? GuideData.fromJson(json['guide_data']) : null;
    appStartDelay = json['app_start_delay'] != null ? json['app_start_delay'] : '0.5';
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.keys != null) {
      data['keys'] = this.keys.toJson();
    }
    if (this.guideImage != null) {
      data['guide_image'] = this.guideImage.toJson();
    }
    return data;
  }
}

class AppConfigKeyModel {
  AppConfigKeyItemModel weibo;
  AppConfigKeyItemModel qq;
  AppConfigKeyItemModel weixin;
  AppConfigKeyItemModel jdIos;
  AppConfigKeyItemModel jdAndroid;
  AppConfigKeyItemModel taobao;
  GDModel gd;

  AppConfigKeyModel({this.weibo, this.qq, this.weixin, this.jdIos, this.jdAndroid,this.taobao});

  AppConfigKeyModel.fromJson(Map<String, dynamic> json) {
    weibo = json['weibo'] != null ? new AppConfigKeyItemModel.fromJson(json['weibo']) : null;
    qq = json['qq'] != null ? new AppConfigKeyItemModel.fromJson(json['qq']) : null;
    weixin = json['weixin'] != null ? new AppConfigKeyItemModel.fromJson(json['weixin']) : null;
    jdIos = json['jd_ios'] != null ? new AppConfigKeyItemModel.fromJson(json['jd_ios']) : null;
    jdAndroid = json['jd_android'] != null ? new AppConfigKeyItemModel.fromJson(json['jd_android']) : null;
    taobao = json['taobao'] != null ? new AppConfigKeyItemModel.fromJson(json['taobao']) : null;
    gd = json['gd'] != null ? GDModel.fromJson(json['gd']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.weibo != null) {
      data['weibo'] = this.weibo.toJson();
    }
    if (this.qq != null) {
      data['qq'] = this.qq.toJson();
    }
    if (this.weixin != null) {
      data['weixin'] = this.weixin.toJson();
    }
    if (this.jdIos != null) {
      data['jd_ios'] = this.jdIos.toJson();
    }
    if (this.jdAndroid != null) {
      data['jd_android'] = this.jdAndroid.toJson();
    }
    if (this.taobao != null) {
      data['taobao'] = this.taobao.toJson();
    }
    if (this.gd != null) {
      data['gd'] = this.gd.toJson();
    }

    return data;
  }
}

class GuideData {
  String indicatorType;
  String indicatorCssType;
  String indicatorChooseColor;
  String indicatorUnchooseColor;
  String btnText;
  String btnBgColor;
  String btnTextColor;
  List<GuideCss> guideCss;

  GuideData({this.indicatorType, this.indicatorCssType, this.indicatorChooseColor, this.indicatorUnchooseColor, this.btnText, this.btnBgColor, this.btnTextColor, this.guideCss});

  GuideData.fromJson(Map<String, dynamic> json) {
    indicatorType = json['indicator_type'];
    indicatorCssType = json['indicator_css_type'];
    indicatorChooseColor = json['indicator_choose_color'];
    indicatorUnchooseColor = json['indicator_unchoose_color'];
    btnText = json['btn_text'];
    btnBgColor = json['btn_bg_color'];
    btnTextColor = json['btn_text_color'];
    if (json['guide_css'] != null) {
      guideCss = new List<GuideCss>();
      json['guide_css'].forEach((v) {
        guideCss.add(new GuideCss.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['indicator_type'] = this.indicatorType;
    data['indicator_css_type'] = this.indicatorCssType;
    data['indicator_choose_color'] = this.indicatorChooseColor;
    data['indicator_unchoose_color'] = this.indicatorUnchooseColor;
    data['btn_text'] = this.btnText;
    data['btn_bg_color'] = this.btnBgColor;
    data['btn_text_color'] = this.btnTextColor;
    if (this.guideCss != null) {
      data['guide_css'] = this.guideCss.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class GuideCss {
  String bgImage;
  String contentImage;

  GuideCss({this.bgImage, this.contentImage});

  GuideCss.fromJson(Map<String, dynamic> json) {
    bgImage = json['bg_image'];
    contentImage = json['content_image'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['bg_image'] = this.bgImage;
    data['content_image'] = this.contentImage;
    return data;
  }
}

class AppConfigKeyItemModel {
  String appId;
  String appkey;
  String secret;
  String redirectUrl;
  String universalLink;
  String androidKey;
  String iosKey;

  AppConfigKeyItemModel({this.appkey, this.secret, this.redirectUrl,this.androidKey,this.iosKey});

  AppConfigKeyItemModel.fromJson(Map<String, dynamic> json) {
    appId = json['app_id'];
    appkey = json['appkey'];
    secret = json['secret'];
    androidKey = json['android_key'];
    iosKey = json['ios_key'];
    redirectUrl = json['redirect_url'];
    universalLink = json['universal_link'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['app_id'] = this.appId;
    data['appkey'] = this.appkey;
    data['secret'] = this.secret;
    data['redirect_url'] = this.redirectUrl;
    data['android_key'] = this.androidKey;
    data['ios_key'] = this.iosKey;
    data['universal_link'] = this.universalLink;
    return data;
  }
}

class AppConfigGuideModel {
  String isShowIndicator;
  List<String> images;

  AppConfigGuideModel({this.isShowIndicator, this.images});

  AppConfigGuideModel.fromJson(Map<String, dynamic> json) {
    isShowIndicator = json['is_show_indicator'];
    images = json['images'].cast<String>();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['is_show_indicator'] = this.isShowIndicator;
    data['images'] = this.images;
    return data;
  }
}

class GDModel {
  String androidKey;
  String iosKey;

  GDModel({this.androidKey, this.iosKey});

  GDModel.fromJson(Map<String, dynamic> json) {
    androidKey = json['android_key'];
    iosKey = json['ios_key'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['android_key'] = this.androidKey;
    data['ios_key'] = this.iosKey;
    return data;
  }
}