import 'package:syncrow_app/features/scene/model/create_automation_model.dart';
import 'package:syncrow_app/features/scene/model/create_scene_model.dart';
import 'package:syncrow_app/features/scene/model/icon_model.dart';
import 'package:syncrow_app/features/scene/model/scene_details_model.dart';
import 'package:syncrow_app/features/scene/model/scenes_model.dart';
import 'package:syncrow_app/features/scene/model/update_automation.dart';
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
import 'package:syncrow_app/services/api/http_service.dart';

class SceneApi {
  static final HTTPService _httpService = HTTPService();

//create scene
  static Future<Map<String, dynamic>> createScene(
      CreateSceneModel createSceneModel) async {
    try {
      final response = await _httpService.post(
        path: ApiEndpoints.createScene,
        body: createSceneModel.toMap(),
        showServerMessage: false,
        expectedResponseModel: (json) {
          return json;
        },
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

// create automation
  static Future<Map<String, dynamic>> createAutomation(
      CreateAutomationModel createAutomationModel, String projectId) async {
    try {
      final response = await _httpService.post(
        path:
            ApiEndpoints.createAutomation.replaceAll('{projectId}', projectId),
        body: createAutomationModel.toMap(),
        showServerMessage: false,
        expectedResponseModel: (json) {
          return json;
        },
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  //get scene by unit id

  static Future<List<ScenesModel>> getScenesByUnitId(
      String unitId, String communityId, String projectId,
      {showInDevice = false}) async {
    try {
      final response = await _httpService.get(
        path: ApiEndpoints.getUnitScenes
            .replaceAll('{spaceUuid}', unitId)
            .replaceAll('{communityUuid}', communityId)
            .replaceAll('{projectUuid}', projectId),
        queryParameters: {'showInHomePage': showInDevice},
        showServerMessage: false,
        expectedResponseModel: (json) {
          final scenesJson = json['data'] as List;

          List<ScenesModel> scenes = [];
          for (var scene in scenesJson) {
            scenes.add(ScenesModel.fromJson(scene));
          }
          return scenes;
        },
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  //getAutomation

  static Future<List<ScenesModel>> getAutomationByUnitId(
    String unitId,
    String communityId,
    String projectId,
  ) async {
    try {
      final response = await _httpService.get(
        path: ApiEndpoints.getUnitAutomation
            .replaceAll('{unitUuid}', unitId)
            .replaceAll('{communityId}', communityId)
            .replaceAll('{projectId}', projectId),
        showServerMessage: false,
        expectedResponseModel: (json) {
          List<ScenesModel> scenes = [];
          for (var scene in json) {
            scenes.add(ScenesModel.fromJson(scene));
          }
          return scenes;
        },
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  static Future<bool> triggerScene(String sceneId) async {
    try {
      final response = await _httpService.post(
        path: ApiEndpoints.triggerScene.replaceAll('{sceneId}', sceneId),
        showServerMessage: false,
        expectedResponseModel: (json) => json['success'],
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

//automation details
  static Future<SceneDetailsModel> getAutomationDetails(
      String automationId, String projectId) async {
    try {
      final response = await _httpService.get(
        path: ApiEndpoints.getAutomationDetails
            .replaceAll('{automationId}', automationId)
            .replaceAll('{projectId}', projectId),
        showServerMessage: false,
        expectedResponseModel: (json) => SceneDetailsModel.fromJson(json),
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  //updateAutomationStatus
  static Future<bool> updateAutomationStatus(String automationId,
      AutomationStatusUpdate createAutomationEnable, String projectId) async {
    try {
      final response = await _httpService.patch(
        path: ApiEndpoints.updateAutomationStatus
            .replaceAll('{automationId}', automationId)
            .replaceAll('{projectId}', projectId),
        body: createAutomationEnable.toMap(),
        expectedResponseModel: (json) => json['success'],
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  static Future<SceneDetailsModel> getSceneDetails(String sceneId) async {
    try {
      final response = await _httpService.get(
        path: ApiEndpoints.getScene.replaceAll('{sceneId}', sceneId),
        showServerMessage: false,
        expectedResponseModel: (json) {
          if (json != null && json['data'] != null) {
            return SceneDetailsModel.fromJson(json['data']);
          } else {
            throw Exception('Data field is null');
          }
        },
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  static Future<List<IconModel>> getIcon() async {
    final response = await _httpService.get(
      path: ApiEndpoints.getIconScene,
      showServerMessage: false,
      expectedResponseModel: (json) {
        List<IconModel> iconsList = [];
        json.forEach((element) {
          iconsList.add(IconModel.fromJson(element));
        });
        return iconsList;
      },
    );
    return response;
  }

  //update Scene
  static updateScene(CreateSceneModel createSceneModel, String sceneId) async {
    try {
      final response = await _httpService.put(
        path: ApiEndpoints.updateScene.replaceAll('{sceneId}', sceneId),
        body: createSceneModel
            .toJson(sceneId.isNotEmpty == true ? sceneId : null),
        expectedResponseModel: (json) {
          return json;
        },
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  //update automation
  static updateAutomation(CreateAutomationModel createAutomationModel,
      String automationId, String projectId) async {
    try {
      final response = await _httpService.put(
        path: ApiEndpoints.updateAutomation
            .replaceAll('{automationId}', automationId)
            .replaceAll('{projectId}', projectId),
        body: createAutomationModel
            .toJson(automationId.isNotEmpty == true ? automationId : null),
        expectedResponseModel: (json) {
          return json;
        },
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  //delete Scene

  static Future<bool> deleteScene({required String sceneId}) async {
    try {
      final response = await _httpService.delete(
        path: ApiEndpoints.deleteScene.replaceAll('{sceneId}', sceneId),
        showServerMessage: false,
        expectedResponseModel: (json) => json['statusCode'] == 200,
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  // delete automation
  static Future<bool> deleteAutomation(
      {required String unitUuid,
      required String automationId,
      required String projectId}) async {
    try {
      final response = await _httpService.delete(
        path: ApiEndpoints.deleteAutomation
            .replaceAll('{automationId}', automationId)
            .replaceAll('{projectId}', projectId),
        showServerMessage: false,
        expectedResponseModel: (json) => json['statusCode'] == 200,
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }
}
