import 'package:sport/bean/page.dart';

import '../Converter.dart';

class Resp {
  int code;
  String msg;

  Resp(this.code, this.msg);

  factory Resp.fromJson(Map<String, dynamic> json) {
    return Resp(json['code'], json['msg']);
  }
}

class RespData<T> extends Resp {
  T data;

  RespData(code, msg, this.data) : super(code, msg);

  factory RespData.fromJson(Map<String, dynamic> json) {
    return RespData(json['code'], json['msg'],
        json['data'] != null ? Converter.fromJson<T>(json['data']) : null);
  }
}

class RespList<T> extends Resp {
  List<T> results;

  RespList(code, msg, this.results) : super(code, msg);

  factory RespList.fromJson(Map<String, dynamic> json) {
    List<T> results = List<T>();
    if (json['data'] != null) {
      (json['data'] as List).forEach((element) {
        results.add(Converter.fromJson<T>(element));
      });
    }
    return RespList(json['code'], json['msg'], results);
  }
}

class PageResult<T> {
  List<T> results;
  Page page;

  PageResult({this.results, this.page});
}

class RespPage<T> extends Resp {
  PageResult<T> pageResult;

  RespPage(code, msg, this.pageResult) : super(code, msg);

  factory RespPage.fromJson(Map<String, dynamic> json) {
    PageResult<T> pageResult = PageResult();
    List<T> results = List();
    if (json['data'] != null) {
      Map<String, dynamic> data = json['data'];
      if (data['list'] != null) {
        (data['list'] as List).forEach((element) {
          results.add(Converter.fromJson<T>(element));
        });
        pageResult.results = results;
      } else if (data['data'] != null) {
        (data['data'] as List).forEach((element) {
          results.add(Converter.fromJson<T>(element));
        });
        pageResult.results = results;
      }
      Page page = Page();
      if (data['pages'] != null) {
        page = Page.fromJson(data['pages'] as Map<String, dynamic>);
        pageResult.page = page;
      }
    }
    return RespPage(json['code'], json['msg'], pageResult);
  }
}

class ResultsTest<T> {
  List<T> list;
  Map map;
}

class RespListTest<T> extends Resp {
  ResultsTest<T> results;

  RespListTest(code, msg, this.results) : super(code, msg);

  factory RespListTest.fromJson(Map<String, dynamic> json) {
    ResultsTest<T> results = ResultsTest<T>();
    Map<String, List<int>> _map = new Map();

    if (json['data'] != null) {
      results.list = [];
      (json['data']["list"] as List).forEach((element) {
        results.list.add(Converter.fromJson<T>(element));
      });

      if (json['data']["map"] != null) {
        results.map  = json['data']["map"];
      }
    }
    print("[results]:${results.map}--------------------------");
    return RespListTest(json['code'], json['msg'], results);
  }
}
