/*
 * @Author: XianKaiQun
 * @Date: 2020-08-03 10:04:31
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-08-12 09:52:37
 * @Description: 
 */
import 'package:app_package/app_package.dart';
import 'package:qrcode_package/src/models/repair_equipment_detail.dart';
import 'package:qrcode_package/src/models/repair_equipment_norm.dart';
import 'package:qrcode_package/src/models/repair_equipment_record.dart';
import 'package:qrcode_package/src/models/repair_plan.dart';
import 'package:qrcode_package/src/service/repair_service.dart';

class RepairServiceImpl implements RepairService {
  static RepairServiceImpl? get instance => _getInstance();
  static RepairServiceImpl? _instance;
  RepairServiceImpl._internal();
  static RepairServiceImpl? _getInstance() {
    if (_instance == null) {
      _instance = RepairServiceImpl._internal();
    }
    return _instance;
  }

  @override
  Future<ResModel<List<RepairPlanModel>?>> getPlans(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/equipment/plans',
      queryParameters: data,
    );

    return ResModel<List<RepairPlanModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<RepairPlanModel>((f) => RepairPlanModel.fromJson(f))
          .toList(),
    );
  }

  @override
  Future<ResModel<RepairEquipmentDetailModel>> getEquipmentDetail(
      String? id) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/equipment/$id',
    );

    return ResModel<RepairEquipmentDetailModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => RepairEquipmentDetailModel.fromJson(v),
    );
  }

  @override
  Future<ResModel<RepairEquipmentRecordModel>> getEquipmentRecord(
      String? opreateRecordId) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/equipment/opreateRecordContent',
      queryParameters: {
        'opreateRecordId': opreateRecordId,
      },
    );

    return ResModel<RepairEquipmentRecordModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => RepairEquipmentRecordModel.fromJson(v),
    );
  }

  @override
  Future<ResModel<RepairEquipmentNormModel>> getEquipmentNorm(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/equipment/${data['id']}/norm',
      queryParameters: {
        'type': data['type'],
      },
    );

    return ResModel<RepairEquipmentNormModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => RepairEquipmentNormModel.fromJson(v),
    );
  }

  @override
  Future<ResModel<int?>> postCheck(data) async {
    var res = await HttpUtil.instance.post(
      'app/admin/equipment/v2/check',
      data: data,
    );

    return ResModel<int?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  @override
  Future<ResModel<int?>> postService(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/equipment/v2/service',
      data: data,
    );

    return ResModel<int?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }
}
