import 'dart:convert';
import 'dart:ui';

import 'package:EasyDartModule/EasyDartModule.dart';
import 'package:ef/ef.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:vbvs_app/common/color/ServiceConstant.dart';
import 'package:vbvs_app/common/color/app_uri_status.dart';
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/common/util/requestWithLog.dart';

import 'package:vbvs_app/controller/mh_controller/device.dart';
import 'package:vbvs_app/model/api_response.dart';

part 'device_list_controller.g.dart';

@JsonSerializable()
class DeviceListModel {
  //设备列表
  List<dynamic> deviceList = [];

  @JsonKey(ignore: true)
  String? keyword;
  @JsonKey(ignore: true)
  Color? color = Color(0xFFFFFFFF);

  DeviceListModel();
  static DeviceListModel fromJson(Map<String, dynamic> json) =>
      _$DeviceListModelFromJson(json);
  Map<String, dynamic> toJson() => _$DeviceListModelToJson(this);
}

class DeviceListController extends GetControllerEx<DeviceListModel> {
  DeviceListController() {
    attr = GetModel(DeviceListModel()).obs;
  }

  getDeviceList() async {
    try {
      String search = (model.keyword != null && model.keyword!.isNotEmpty)
          ? "?key=${model.keyword}&ncs=1"
          : "?ncs=1";
      ApiResponse apiResponse = ApiResponse(code: -1, msg: "设备.设备列表请求失败".tr);
      String serviceAddress = ServiceConstant.service_address;
      String serviceName = ServiceConstant.server_service;
      String serviceApi = ServiceConstant.device_list;
      String queryUrl = "${serviceAddress}${serviceName}${serviceApi}$search";
      var response = await EasyDartModule.dio.get(queryUrl);
      if (response != null) {
        var responseData =
            response.data is String ? jsonDecode(response.data) : response.data;
        ApiResponse res =
            ApiResponse.fromJson(responseData, (object) => object);
        MyUtils.formatResponse(res, "设备.设备列表请求成功".tr, "设备.设备列表请求失败".tr);
        if (res.code == HttpStatusCodes.ok) {
          // bindDeviceNum.value = res.total!;
          model.deviceList = res.data;

          updateAll();
          return res;
        }
      } else {
        return ApiResponse(code: -1, msg: "服务器.失败".tr);
      }
      return apiResponse;
    } catch (e) {
      EasyDartModule.logger.info("设备请求列表: $e");
      DailyLogUtils.writeLog("设备请求列表: $e");
    }
    return ApiResponse(code: -1, msg: "未知错误".tr); // Default return statement
  }

//主动解绑设备
  Future<String> unbindDevice(Map<dynamic, dynamic> device) async {
    String serviceAddress = ServiceConstant.service_address;
    String serviceName = ServiceConstant.server_service;
    String serviceApi = ServiceConstant.device_bind;
    String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
    String msg = '';
    int type = device['bind_type'];
    await requestWithLog(
      logTitle: '解绑设备',
      method: MyHttpMethod.delete,
      queryUrl: queryUrl,
      data: {"mac": device['mac']},
      onSuccess: (res) {
        if (type == 1) {
          msg = '解绑成功'.tr;
        } else {
          msg = '删除成功'.tr;
        }
      },
      onFailure: (res) {
        if (type == 1) {
          msg = '解绑失败'.tr;
        } else {
          msg = '删除失败'.tr;
        }
      },
    );
    return msg;
  }
}
