import 'package:dio/dio.dart';
import 'package:qadirneyriz/config/config.dart';
import 'package:qadirneyriz/models/meetings/meetings_location_model.dart';
import 'package:qadirneyriz/models/meetings/meetings_managers_model.dart';
import 'package:qadirneyriz/models/meetings/meetings_subjects_model.dart';
import 'package:qadirneyriz/models/meetings/meetings_users_model.dart';
import 'package:qadirneyriz/setting/setting.dart';
import 'package:qadirneyriz/utils/result/result.dart';

class GlobalServices {
  // location
  Future<List<LocationsModel>> getLocation() async {
    Map<String, String> headers = {
      'Accept': 'application/json',
    };

    final String link =
        "${config.network.baseUrl}admin/locations?lang=${setting.userLocalDb.getUser().language}";

    final response = await Dio().get(link,
        options: Options(
          headers: headers,
        ));
    final list = response.data
        .map<LocationsModel>((e) => LocationsModel.fromJson(e))
        .toList();
    return list;
  }

// subjects
  Future<List<SubjectsModel>> getSubjects() async {
    Map<String, String> headers = {
      'Accept': 'application/json',
    };

    final String link =
        "${config.network.baseUrl}admin/subjects?lang=${setting.userLocalDb.getUser().language}";

    final response = await Dio().get(link,
        options: Options(
          headers: headers,
        ));
    final list = response.data
        .map<SubjectsModel>((e) => SubjectsModel.fromJson(e))
        .toList();

    return list;
  }

// managers
  Future<List<MeetingsMangersModel>> getManagers() async {
    Map<String, String> headers = {
      'Accept': 'application/json',
    };
    String dataToken = setting.userLocalDb.getUser().token!;
    if (dataToken != '') {
      headers['Authorization'] = "Bearer $dataToken";
    }

    final String link =
        "${config.network.baseUrl}meeting-manager?lang=${setting.userLocalDb.getUser().language}";

    final response = await Dio().get(link,
        options: Options(
          headers: headers,
        ));
    final list = response.data
        .map<MeetingsMangersModel>((e) => MeetingsMangersModel.fromJson(e))
        .toList();
    return list;
  }

// users
  Future<List<UsersModel>> getUsers() async {
    Map<String, String> headers = {
      'Accept': 'application/json',
    };
    String dataToken = setting.userLocalDb.getUser().token!;
    if (dataToken != '') {
      headers['Authorization'] = "Bearer $dataToken";
    }

    final String link =
        "${config.network.baseUrl}admin/users?lang=${setting.userLocalDb.getUser().language}";

    final response = await Dio().get(link,
        options: Options(
          headers: headers,
        ));
    final list =
        response.data.map<UsersModel>((e) => UsersModel.fromJson(e)).toList();
    return list;
  }

// add new subject
  Future<Result> addNewSubject({
    required String subject,
    required String enSubject,
  }) async {
    try {
      Map<String, String> headers = {"Accept": "application/json"};
      FormData? formData;

      String dataToken = setting.userLocalDb.getUser().token!;
      if (dataToken != '') {
        headers['Authorization'] = "Bearer $dataToken";
      }
      formData =
          FormData.fromMap({"subject": subject, "subject_en": enSubject});

      final res = await Dio().post("${config.network.baseUrl}admin/add-subject",
          data: formData, options: Options(headers: headers));

      if (res.statusCode == 200 || res.statusCode == 201) {
        return Result(isOk: true, message: res.data['msg']);
      }
    } on DioException catch (e) {
      return Result(
          isOk: false,
          errors: e.response!.data['errors'],
          message: e.response!.data['msg']);
    }
    return const Result(isOk: false);
  }

// add new user
  Future<Result> addNewUser({
    required String mobile,
    required String name,
    required String password,
    required int role,
  }) async {
    try {
      Map<String, String> headers = {"Accept": "application/json"};
      FormData? formData;

      String dataToken = setting.userLocalDb.getUser().token!;
      if (dataToken != '') {
        headers['Authorization'] = "Bearer $dataToken";
      }
      formData = FormData.fromMap(
          {"mobile": mobile, "name": name, "password": password, "role": role});

      final res = await Dio().post("${config.network.baseUrl}admin/add-user",
          data: formData, options: Options(headers: headers));

      if (res.statusCode == 200 || res.statusCode == 201) {
        return Result(isOk: true, message: res.data['msg']);
      }
    } on DioException catch (e) {
      return Result(
          isOk: false,
          errors: e.response!.data['errors'],
          message: e.response!.data['msg']);
    }
    return const Result(isOk: false);
  }

// add new location
  Future<Result> addNewLocation({
    required String address,
    required String addressEn,
  }) async {
    try {
      Map<String, String> headers = {"Accept": "application/json"};
      FormData? formData;

      String dataToken = setting.userLocalDb.getUser().token!;
      if (dataToken != '') {
        headers['Authorization'] = "Bearer $dataToken";
      }
      formData =
          FormData.fromMap({"address": address, "address_en": addressEn});

      final res = await Dio().post(
          "${config.network.baseUrl}admin/add-location",
          data: formData,
          options: Options(headers: headers));

      if (res.statusCode == 200 || res.statusCode == 201) {
        return Result(isOk: true, message: res.data['msg']);
      }
    } on DioException catch (e) {
      return Result(
          isOk: false,
          errors: e.response!.data['errors'],
          message: e.response!.data['msg']);
    }
    return const Result(isOk: false);
  }
}
