import 'dart:convert';

import 'package:zhiying_base_widget/pages/feedback_page/model/feedback_record_data_model.dart';
import 'package:zhiying_base_widget/pages/feedback_page/model/feedback_record_style_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class FeedbackRecordRepository {
  // final int max = 5;
  int _currentPage = 1;
  bool _hasMore = true;

  final Map<String, dynamic> data;

  FeedbackRecordRepository(this.data) {
    _dataModel = FeedbackRecordDataModel();
    _dataModel.data = [];
  }

  /// 样式数据
  FeedbackRecordStyleModel _styleModel;

  /// 数据
  FeedbackRecordDataModel _dataModel;

  FeedbackRecordStyleModel get styleModel => _styleModel;

  FeedbackRecordDataModel get dataModel => _dataModel;

  /// 初始化数据
  Future<dynamic> fetchInitData() async {
    _hasMore = true;
    _currentPage = 1;
    return _fetchBaseRequest();
  }

  /// 样式初始化
  Future<dynamic> fetchInitStyle() async {
    try {
      String skipIdentifier =
          !EmptyUtil.isEmpty(data) && data.containsKey('skip_identifier') && !EmptyUtil.isEmpty(data['skip_identifier']) ? data['skip_identifier'] : 'pub.flutter.feedback_list';
      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 _handleStyle(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 获取缓存样式
  Future<dynamic> fetchCacheStyle() async {
    try {
      String skipIdentifier =
          !EmptyUtil.isEmpty(data) && data.containsKey('skip_identifier') && !EmptyUtil.isEmpty(data['skip_identifier']) ? data['skip_identifier'] : 'pub.flutter.feedback_list';
      var result = await NetUtil.getRequestCachedData('/api/v1/mod/$skipIdentifier');
      if (!EmptyUtil.isEmpty(result)) {
        return _handleStyle(result);
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 处理样式
  dynamic _handleStyle(Map<String, dynamic> data) {
    try {
      var modListData = data['mod_list'][0]['data'];
      if (!EmptyUtil.isEmpty(modListData)) {
        FeedbackRecordStyleModel model = FeedbackRecordStyleModel.fromJson(modListData is String ? jsonDecode(modListData) : modListData);
        if (!EmptyUtil.isEmpty(model)) {
          _styleModel = model;
          return _styleModel;
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// 刷新
  Future<dynamic> fetchRefreshData() async {
    _hasMore = true;
    _currentPage = 1;
    return _fetchBaseRequest();
  }

  /// 加载更多
  Future<dynamic> fetchLoadData() async {
    if (_hasMore) {
      return _fetchBaseRequest();
    }
    return null;
  }

  /// 基础加载方法
  Future<dynamic> _fetchBaseRequest() async {
    try {
      var result = await NetUtil.post('/api/v1/feedback/list', method: NetMethod.GET, queryParameters: {'page': _currentPage.toString()});
      if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
        FeedbackRecordDataModel model = FeedbackRecordDataModel.fromJson(result);
        if (!EmptyUtil.isEmpty(model) && !EmptyUtil.isEmpty(model?.data)) {
          _dataModel.data.addAll(model.data);
          _hasMore = true;
          ++_currentPage;
          return _dataModel;
        }
      }
      _hasMore = false;
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }
}
