import 'dart:async';
import 'package:car_provider_app/classes/app_state.dart';
import 'package:car_provider_app/models/m_response.dart';
import 'package:car_provider_app/models/profile/branch.dart';
import 'package:car_provider_app/models/profile/branch2.dart';
import 'package:car_provider_app/models/profile/categroy.dart';
import 'package:car_provider_app/models/profile/document.dart';
import 'package:car_provider_app/models/profile/services.dart';
import 'package:car_provider_app/models/user/country.dart';
import 'package:car_provider_app/models/user/role.dart';
import 'package:http/http.dart';
import 'package:car_provider_app/classes/consts.dart';
import 'package:car_provider_app/models/content_info_model.dart';
import 'package:car_provider_app/models/member_model.dart';
import 'package:car_provider_app/models/surah_model.dart';
import 'package:car_provider_app/models/user/basic_otp.dart';
import 'package:car_provider_app/models/user/register_user.dart';

import '../api_client.dart';

class BranchApiClent {
  static final BranchApiClent _instance = BranchApiClent._internal();

  BranchApiClent._internal();

  factory BranchApiClent() => _instance;

  Future<Branch> fetchBranch() async {
    var postParams = {
      "serviceProviderID": AppState().getUser.data?.userInfo?.providerId.toString() ?? "",
    };
    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().getJsonForObject((json) => Branch.fromJson(json), ApiConsts.getProviderBranch, queryParameters: postParams, token: t);
  }

  Future<MResponse> createBranch(String branchName, String branchDescription, String cityId, String address, String latitude, String longitude) async {
    String lat = "0", long = "0";
    try {
      lat = latitude.substring(0, 9);
      long = longitude.substring(0, 9);
    } catch (e) {}
    var postParams = {
      // "id": 0,
      "serviceProviderID": AppState().getUser.data?.userInfo?.providerId ?? "",
      "branchName": branchName,
      "branchDescription": branchDescription,
      "cityID": cityId,
      "address": address,
      "latitude": lat,
      "longitude": long,
      "isActive": true
    };
    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().postJsonForObject((json) => MResponse.fromJson(json), ApiConsts.createProviderBranch, postParams, token: t);
  }

  Future<MResponse> updateBranch(int id, String branchName, String branchDescription, String cityId, String address, String latitude, String longitude, {bool isNeedToDelete = true}) async {
    String lat = "0", long = "0";
    try {
      lat = latitude.substring(0, 9);
      long = longitude.substring(0, 9);
    } catch (e) {}
    var postParams = {
      "id": id,
      "serviceProviderID": AppState().getUser.data?.userInfo?.providerId ?? "",
      "branchName": branchName,
      "branchDescription": branchDescription,
      "cityID": cityId,
      "address": address,
      "latitude": lat,
      "longitude": long,
      "isActive": isNeedToDelete
    };
    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().postJsonForObject((json) => MResponse.fromJson(json), ApiConsts.updateProviderBranch, postParams, token: t);
  }

  Future<MResponse> deleteBranch(int id) async {
    var postParams = {"id": id, "serviceProviderID": AppState().getUser.data?.userInfo?.providerId ?? "", "isActive": false};
    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().postJsonForObject((json) => MResponse.fromJson(json), ApiConsts.updateProviderBranch, postParams, token: t);
  }

  Future<Branch> fetchAllBranches() async {
    var postParams = {"ServiceProviderID": AppState().getUser.data?.userInfo?.providerId.toString() ?? ""};
    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().getJsonForObject((json) => Branch.fromJson(json), ApiConsts.ServiceProviderBranchGet, queryParameters: postParams, token: t);
  }

  Future<Category> fetchBranchCategory() async {
    var postParams = {"ServiceProviderID": AppState().getUser.data?.userInfo?.providerId.toString() ?? ""};
    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().getJsonForObject((json) => Category.fromJson(json), ApiConsts.ServiceCategory_Get, queryParameters: postParams, token: t);
  }

  Future<Services> fetchServices(String serviceCategoryId) async {
    var postParams = {"ServiceCategoryID": serviceCategoryId};
    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().getJsonForObject((json) => Services.fromJson(json), ApiConsts.Services_Get, queryParameters: postParams, token: t);
  }

  Future<MResponse> createService(List<Map<String, dynamic>> map) async {
    String t = AppState().getUser.data!.accessToken ?? "";
    return await ApiClient().postJsonForObject((json) => MResponse.fromJson(json), ApiConsts.ServiceProviderService_Create, map, token: t);
  }

  Future<MResponse> updateService(List<Map<String, dynamic>> map) async {
    String t = AppState().getUser.data!.accessToken ?? "";
    return await ApiClient().postJsonForObject((json) => MResponse.fromJson(json), ApiConsts.ServiceProviderService_Update, map, token: t);
  }

  Future<MResponse> getServiceProviderService() async {
    var postParams = {"ProviderBranchID": "9"};
    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().getJsonForObject((json) => MResponse.fromJson(json), ApiConsts.ServiceProviderService_Get, queryParameters: postParams, token: t);
  }

  Future<Branch2> getBranchAndServices() async {
    var postParams = {"serviceProviderID": AppState().getUser.data?.userInfo?.providerId.toString() ?? ""};
    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().getJsonForObject((json) => Branch2.fromJson(json), ApiConsts.BranchesAndServices, queryParameters: postParams, token: t);
  }
}
