import 'dart:convert';

import 'package:mohem_flutter_app/api/api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/consts.dart';
import 'package:mohem_flutter_app/models/items_for_sale/get_employee_ads_list.dart';
import 'package:mohem_flutter_app/models/items_for_sale/get_items_for_sale_list.dart';
import 'package:mohem_flutter_app/models/items_for_sale/get_regions_list.dart';
import 'package:mohem_flutter_app/models/items_for_sale/get_sale_categories_list.dart';
import 'package:mohem_flutter_app/models/items_for_sale/item_review_model.dart';

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

  ItemsForSaleApiClient._internal();

  factory ItemsForSaleApiClient() => _instance;

  Future<List<GetSaleCategoriesList>> getSaleCategories() async {
    List<GetSaleCategoriesList> getSaleCategoriesList = [];

    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetItemSaleCategory";
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER, "ItgPageSize": 10, "ItgPageNo": 1};

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((response) {
      var body = json.decode(response['Mohemm_ITG_ResponseItem']);

      GetSaleCategoriesList getSaleCategoriesListObj = new GetSaleCategoriesList();
      getSaleCategoriesListObj.categoryID = 0;
      getSaleCategoriesListObj.title = "All";
      getSaleCategoriesListObj.titleAr = "الجميع";
      getSaleCategoriesListObj.isActive = true;
      getSaleCategoriesListObj.content =
          '<svg xmlns="http://www.w3.org/2000/svg" width="33.925" height="25.841" viewBox="0 0 33.925 25.841"><g id="More_Select" data-name="More Select"><path d="m30 1h-24a1 1 0 0 0 -1 1v1h21a3 3 0 0 1 3 3v21h1a1 1 0 0 0 1-1v-24a1 1 0 0 0 -1-1z"/><path d="m26 5h-24a1 1 0 0 0 -1 1v24a1 1 0 0 0 1 1h24a1 1 0 0 0 1-1v-24a1 1 0 0 0 -1-1zm-4.747 9.344-8.728 8.726a1 1 0 0 1 -1.414 0l-4.364-4.363a1 1 0 0 1 1.414-1.414l3.657 3.656 8.021-8.019a1 1 0 0 1 1.414 1.414z"/></g></svg>';

      getSaleCategoriesList.add(getSaleCategoriesListObj);

      body['result']['data'].forEach((v) {
        getSaleCategoriesList.add(new GetSaleCategoriesList.fromJson(v));
      });
      return getSaleCategoriesList;
    }, url, postParams);
  }

  Future<List<GetItemsForSaleList>> getItemsForSale(int itgPageNo, int itgCategoryID) async {
    List<GetItemsForSaleList> getItemsForSaleList = [];

    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetItemForSale";
    Map<String, dynamic> postParams = {
      "EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER,
      "ItgPageSize": 10,
      "ItgPageNo": itgPageNo,
      "ItgStatus": "Approved",
      "ItgCategoryID": itgCategoryID
    };

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((response) {
      var body = json.decode(response['Mohemm_ITG_ResponseItem']);

      body['result']['data'].forEach((v) {
        getItemsForSaleList.add(new GetItemsForSaleList.fromJson(v));
      });
      return getItemsForSaleList;
    }, url, postParams);
  }

  Future<dynamic> updateItemsForSale(int itemSaleID) async {
    List<GetItemsForSaleList> getItemsForSaleList = [];

    String url = "${ApiConsts.cocRest}Mohemm_ITG_UpdateItemForSale";

    // request.fields['itemSaleID'] = itemSaleID.toString();
    // request.fields['Channel'] = "31";
    // request.fields['isActive'] = "false";
    // request.fields['LogInToken'] = loginTokenID!;
    // request.fields['Token'] = tokenID!;
    // request.fields['MobileNo'] = empMobNum!;
    // request.fields['EmployeeNumber'] = empNum!;
    // request.fields['employeeNumber'] = empNum;

    Map<String, dynamic> postParams = {
      "EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER,
      "employeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER,
      "MobileNo": AppState().memberInformationList?.eMPLOYEEMOBILENUMBER,
      "itemSaleID": itemSaleID.toString(),
      "Channel": "31",
      "isActive": "false",
      "Token": AppState().postParamsObject?.tokenID
    };

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((response) {
      var body = json.decode(response['Mohemm_ITG_ResponseItem']);

      // body['result']['data'].forEach((v) {
      //   getItemsForSaleList.add(new GetItemsForSaleList.fromJson(v));
      // });
      return getItemsForSaleList;
    }, url, postParams);
  }

  Future<List<EmployeePostedAds>> getEmployeePostedAds() async {
    List<EmployeePostedAds> employeePostedAdsList = [];

    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetItemForSaleByEmployee";
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER, "ItgEmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER};

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((response) {
      var body = json.decode(response['Mohemm_ITG_ResponseItem']);

      body['result']['data'].forEach((v) {
        employeePostedAdsList.add(new EmployeePostedAds.fromJson(v));
      });
      return employeePostedAdsList;
    }, url, postParams);
  }

  Future<List<GetRegionsList>> getRegions() async {
    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetRegion";
    List<GetRegionsList> getRegionsList = [];
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER, "ItgEmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((response) {
      var body = json.decode(response['Mohemm_ITG_ResponseItem']);

      body['result']['data'].forEach((v) {
        getRegionsList.add(new GetRegionsList.fromJson(v));
      });
      return getRegionsList;
    }, url, postParams);
  }

  Future<String> addItemForSale(ItemReviewModel itemReviewModel, List<Map<String, dynamic>> imagesList) async {
    String url = "${ApiConsts.cocRest}Mohemm_ITG_AddItemForSaleMobile";
    Map<String, dynamic> postParams = {
      "ItgImageCollList": imagesList,
      "ItgTitle": itemReviewModel.itemTitle,
      "ItgTitleAr": itemReviewModel.itemTitle,
      "ItgCategoryID": itemReviewModel.selectedSaleCategory!.categoryID,
      "ItgDescription": itemReviewModel.itemDescription,
      "ItgDescriptionAr": itemReviewModel.itemDescription,
      "ItgQuotePrice": itemReviewModel.itemPrice,
      "RegionID": itemReviewModel.selectedRegion!.regionID,
      "ItgIsActive": true,
      "EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER,
      "employeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER,
      "ItgStatus": itemReviewModel.itemCondition
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((response) {
      var body = json.decode(response['Mohemm_ITG_ResponseItem']);
      return body["message"];
    }, url, postParams);
  }
}
