import 'dart:convert';

import 'package:zhiying_base_widget/pages/wechat_teacher_page/model/wechat_teacher_data_model.dart';
import 'package:zhiying_base_widget/pages/wechat_teacher_page/model/wechat_teacher_style_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class WechatTeacherRepository {
  final Map<String, dynamic> data;

  WechatTeacherRepository(this.data);

  WechatTeacherStyleModel _styleModel;
  WechatTeacherDataModel _dataModel;

  WechatTeacherStyleModel get styleModel => _styleModel;

  WechatTeacherDataModel get dataModel => _dataModel;

  /// 初始化
  Future<WechatTeacherStyleModel> fetchInitStyle() async {
    try {
      String skipIdentifier =
          !EmptyUtil.isEmpty(data) && data.containsKey('skip_identifier') && !EmptyUtil.isEmpty(data['skip_identifier']) ? data['skip_identifier'] : 'pub.flutter.wechat_teacher';

      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])) {
        return handleStyleModel(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  Future<WechatTeacherStyleModel> fetchCachedStyle() async {
    try {
      String skipIdentifier =
          !EmptyUtil.isEmpty(data) && data.containsKey('skip_identifier') && !EmptyUtil.isEmpty(data['skip_identifier']) ? data['skip_identifier'] : 'pub.flutter.wechat_teacher';
      var result = await NetUtil.getRequestCachedData('/api/v1/mod/$skipIdentifier');
      if (!EmptyUtil.isEmpty(result)) {
        return handleStyleModel(result);
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  WechatTeacherStyleModel handleStyleModel(final Map<String, dynamic> result) {
    var modListData = result['mod_list'][0]['data'];
    if (!EmptyUtil.isEmpty(modListData)) {
      WechatTeacherStyleModel model = WechatTeacherStyleModel.fromJson(modListData is String ? jsonDecode(modListData) : modListData);
      if (!EmptyUtil.isEmpty(model)) {
        _styleModel = model;
      }
      return _styleModel;
    }
    return null;
  }

  Future<WechatTeacherDataModel> fetchInitData() async {
    try {
      var result = await NetUtil.post('/api/v1/user/wx_teacher', method: NetMethod.GET);
      if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
        WechatTeacherDataModel model = WechatTeacherDataModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
        if (!EmptyUtil.isEmpty(model)) {
          _dataModel = model;
        }
        return _dataModel;
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 绑定导师
  Future<WechatTeacherDataModel> bindTeacher(final String wxchatAccount) async {
    try {
      var result = await NetUtil.post('/api/v1/user/myteam/relate', method: NetMethod.POST, params: {'invite_code': wxchatAccount});
      if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
        return await fetchInitData();
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }
}
