/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-04-28 11:21:51
 * @LastEditTime : 2023-05-15 15:10:48
 * @Description  : Do not edit
 */
import 'package:app_package/app_package.dart';
import 'package:smart_devices_package/src/models/sd_detail_list_model.dart';
import 'package:smart_devices_package/src/models/sd_timing_list.dart';
import 'package:smart_devices_package/src/models/timing_detail.dart';
import 'package:smart_devices_package/src/models/timing_type.dart';

import '../sd_service.dart';
import '../../models/sd_list_model.dart';
import '../../models/sd_detail_model.dart';

class SmartDevicesServiceImpl implements SmartDevicesService {
  static String prefix = '/app/admin';

  /// 智能设备-列表
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/20382
  @override
  Future<ResModel<PagingModel<SmartDevicesListModel>>> getSmartDevicesList(
      data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/smartDevice',
      queryParameters: data,
    );
    return ResModel<PagingModel<SmartDevicesListModel>>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => PagingModel(
        json: v,
        to: (v) => v.toJson(),
        from: (v) => SmartDevicesListModel.fromJson(v),
      ),
    );
  }

  /// 智能设备-详情
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/20387
  Future<ResModel<SmartDevicesDetailModel>> getDeviceDetail(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/smartDevice/${data['id']}',
    );

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

  /// 智能设备-操作记录
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/20402
  Future<ResModel<PagingModel<SmartDevicesDetailListModel>>> getDeviceRecord(
      data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/smartDevice/record',
      queryParameters: data,
    );

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

  ///智能设备-操作
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/20392

  Future<bool> getDeviceOnAndOff(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/smartDevice/operate',
      queryParameters: data,
    );

    if (res.data['data'] != null && res.data['data'] is bool) {
      return res.data['data'];
    } else {
      return false;
    }
  }

  /// 【定时管理 全部接口】 http://yapi.wisdomcity.com.cn/project/40/interface/api/cat_1196
  ///
  ///
  ///
  ///  定时管理列表
  Future<ResModel<PagingModel<TimingListModel>>> getTimingList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/schemes',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  /// 定时管理启用
  Future<ResModel<bool>> patchTimingEnable(id) async {
    var res = await HttpUtil.instance.patch(
      '/app/admin/schemes/$id/enable',
    );

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

  /// 定时管理停用
  Future<ResModel<bool>> patchTimingDisable(id) async {
    var res = await HttpUtil.instance.patch(
      '/app/admin/schemes/$id/diable',
    );

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

  /// 定时管理删除
  Future<ResModel<bool>> deleteTiming(id) async {
    var res = await HttpUtil.instance.delete(
      '/app/admin/schemes/$id',
    );

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

  /// 定时管理添加
  Future<ResModel<bool>> addTiming(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/schemes',
      data: data,
    );

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

  /// 定时管理编辑
  Future<ResModel<bool>> editTiming(data, id) async {
    var res = await HttpUtil.instance.put('/app/admin/schemes/$id', data: data);

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

  /// 定时类型
  Future<ResModel<List<TimingTypeModel>>> getTimingType() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/schemes/task/dict',
    );

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

  /// 定时详情
  Future<ResModel<TimingDetailModel>> getTimingDetail(id) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/schemes/$id',
    );

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