import 'dart:convert';

import 'package:dio/src/form_data.dart' as formdata;
import 'package:ef/ef.dart';
import 'package:json_annotation/json_annotation.dart';

part 'sleeping_habit_controller.g.dart'; // 由json_serializable自动生成的部分

@JsonSerializable()
class SleepingHabitModel {
  bool rxhxIsStart = false;
  List<int> rxhxWakeTime = [0, 0];
  bool rxhxIsBell = false;
  bool rxhxIsAnMo = false;
  int rxhxLocation = 0;
  List rxhxWeeks = [0, 0, 0, 0, 0, 0, 0];

  bool dhgyIsStart = false;

  bool smysIsStart = false;
  List<int> smysStartTime = [20, 0];
  List<int> smysEndTime = [9, 0];

  String get rxhxWakeTimeToString {
    return '${rxhxWakeTime[0]}'.padLeft(2, "0") +
        ":" +
        '${rxhxWakeTime[1]}'.padLeft(2, "0");
  }

  String get smysStartTimeToString {
    return '${smysStartTime[0]}'.padLeft(2, "0") +
        ":" +
        '${smysStartTime[1]}'.padLeft(2, "0");
  }

  String get smysEndTimeToString {
    return '${smysEndTime[0]}'.padLeft(2, "0") +
        ":" +
        '${smysEndTime[1]}'.padLeft(2, "0");
  }

  SleepingHabitModel();

  factory SleepingHabitModel.fromJson(Map<String, dynamic> json) {
    try {
      return _$SleepingHabitModelFromJson(json);
    } catch (e) {
      print("$e");
      // 在实际应用中，应该有更细致的异常处理策略和错误日志
      return SleepingHabitModel(); // 或者返回一个带有错误信息的特定AboutModel实例
    }
  }
  Map<String, dynamic> toJson() => _$SleepingHabitModelToJson(this);
}

class SleepingHabitController extends GetControllerEx<SleepingHabitModel> {
  SleepingHabitController() {
    attr = GetModel(SleepingHabitModel()).obs;
  }

  // saveDataApi() {
  //   String mac = Get.find<GlobalController>().model.deviceMain["mac"];
  //   ApiService.request.post("/api/device/info/config",
  //       data: formdata.FormData.fromMap({
  //         "mac": mac,
  //         "type": "sleepHabit",
  //         "data": jsonEncode(model.toJson())
  //       }));
  // }

  // loadDataApi({int time = 3}) {
  //   String mac = Get.find<GlobalController>().model.deviceMain["mac"];

  //   ApiService.request
  //       .get("/api/device/info/config?type=sleepHabit&mac=${mac}")
  //       .then((d) async {
  //     if (d.data["data"] != null && d.data["data"]["data"] != null) {
  //       attr.value.model =
  //           SleepingHabitModel.fromJson(jsonDecode(d.data["data"]["data"]));
  //       print("load ${model.toJson()}");
  //     } else {
  //       attr.value.model = SleepingHabitModel();
  //       print("load error");
  //     }
  //     updateAll();
  //   }).catchError((d) {
  //     if (time > 0) {
  //       loadDataApi(time: time - 1);
  //     } else {
  //       attr.value.model = SleepingHabitModel();
  //     }
  //   });
  // }
}
