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/offers_and_discounts/get_categories_list.dart';
import 'package:mohem_flutter_app/models/offers_and_discounts/get_offers_list.dart';

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

  OffersAndDiscountsApiClient._internal();

  factory OffersAndDiscountsApiClient() => _instance;

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

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

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

        GetCategoriesList getSaleCategoriesListObj = GetCategoriesList();
        getSaleCategoriesListObj.id = 0;
        getSaleCategoriesListObj.categoryNameEn = "All";
        getSaleCategoriesListObj.categoryNameAr = "الجميع";
        getSaleCategoriesListObj.isActive = true;
        getSaleCategoriesListObj.content =
            '<svg xmlns="http://www.w3.org/2000/svg" width="26.213" height="26.213" viewBox="0 0 26.213 26.213"> <g id="More_Select" data-name="More Select" transform="translate(-1 -1)"> <path id="Path_4860" data-name="Path 4860" d="M26.844,1H5.874A.874.874,0,0,0,5,1.874v.874H23.349A2.621,2.621,0,0,1,25.97,5.369V23.718h.874a.874.874,0,0,0,.874-.874V1.874A.874.874,0,0,0,26.844,1Z" transform="translate(-0.505)" fill="#2bb8a6"/> <path id="Path_4861" data-name="Path 4861" d="M22.844,5H1.874A.874.874,0,0,0,1,5.874v20.97a.874.874,0,0,0,.874.874h20.97a.874.874,0,0,0,.874-.874V5.874A.874.874,0,0,0,22.844,5ZM18.7,13.164,11.07,20.789a.874.874,0,0,1-1.236,0L6.022,16.977a.874.874,0,1,1,1.236-1.236l3.2,3.195,7.008-7.007A.874.874,0,1,1,18.7,13.165Z" transform="translate(0 -0.505)" fill="#125765"/> </g> </svg>';

        getSaleCategoriesList.add(getSaleCategoriesListObj);

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

  Future<List<OffersListModel>> getOffersList(int categoryID, int pageSize) async {
    List<OffersListModel> getSaleCategoriesList = [];

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

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

        var bodyData = body['result']['data'];

        if (bodyData != null) {
          bodyData.forEach((v) {
            getSaleCategoriesList.add(OffersListModel.fromJson(v));
          });
        }
        return getSaleCategoriesList;
      },
      url,
      postParams,
    );
  }
}
