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};

    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="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(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 = json.decode(body['result']['data']);

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