import 'dart:async';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/auth/model/user_model.dart';
import 'package:syncrow_app/features/menu/model/region_model.dart';
import 'package:syncrow_app/features/menu/model/time_zone_model.dart';
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
import 'package:syncrow_app/services/api/http_service.dart';

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

  static Future<Map<String, dynamic>> saveName({
    String? firstName,
    String? lastName,
  }) async {
    try {
      final response = await _httpService.put(
        path: ApiEndpoints.saveName
            .replaceAll('{userUuid}', HomeCubit.user!.uuid!),
        body: {"firstName": firstName, "lastName": lastName},
        expectedResponseModel: (json) {
          return json;
        },
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  static Future saveRegion({
    String? regionUuid,
  }) async {
    try {
      final response = await _httpService.put(
        path: ApiEndpoints.saveRegion
            .replaceAll('{userUuid}', HomeCubit.user!.uuid!),
        body: {
          "regionUuid": regionUuid,
        },
        expectedResponseModel: (json) {
          return json;
        },
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  static Future saveTimeZone({
    String? regionUuid,
  }) async {
    try {
      final response = await _httpService.put(
        path: ApiEndpoints.saveTimeZone
            .replaceAll('{userUuid}', HomeCubit.user!.uuid!),
        body: {
          "timezoneUuid": regionUuid,
        },
        expectedResponseModel: (json) {
          return json;
        },
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  static Future<Map<String, dynamic>> saveImage(String image) async {
    try {
      final response = await _httpService.put(
        path: ApiEndpoints.sendPicture
            .replaceAll('{userUuid}', HomeCubit.user!.uuid!),
        body: {"profilePicture": 'data:image/png;base64,$image'},
        expectedResponseModel: (json) {
          return json;
        },
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }

  Future fetchUserInfo(userId) async {
    final response = await _httpService.get(
        path: ApiEndpoints.getUser.replaceAll('{userUuid}', userId!),
        showServerMessage: true,
        expectedResponseModel: (json) {
          return UserModel.fromJson(json);
        });
    return response;
  }

  Future fetchPermissions(roleId) async {
    final response = await _httpService.get(
        path: ApiEndpoints.getPermission.replaceAll('{roleUuid}', roleId!),
        showServerMessage: true,
        expectedResponseModel: (json) {
          return json;
        });
    return response;
  }

  static Future<List<RegionModel>> fetchRegion() async {
    final response = await _httpService.get(
        path: ApiEndpoints.getRegion,
        showServerMessage: true,
        expectedResponseModel: (json) {
          return (json as List)
              .map((zone) => RegionModel.fromJson(zone))
              .toList();
        });
    return response as List<RegionModel>;
  }

  static Future<List<TimeZone>> fetchTimeZone() async {
    final response = await _httpService.get(
        path: ApiEndpoints.getTimezone,
        showServerMessage: true,
        expectedResponseModel: (json) {
          return (json as List).map((zone) => TimeZone.fromJson(zone)).toList();
        });
    return response as List<TimeZone>;
  }

  Future fetchUserAgreement() async {
    final response = await _httpService.get(
        path: ApiEndpoints.terms,
        showServerMessage: true,
        expectedResponseModel: (json) {
          return json['data'];
        });
    return response;
  }

  Future fetchPrivacyPolicy() async {
    final response = await _httpService.get(
        path: ApiEndpoints.policy,
        showServerMessage: true,
        expectedResponseModel: (json) {
          return json['data'];
        });
    return response;
  }
}
