import 'dart:async';

import 'package:tangheem/classes/consts.dart';
import 'package:tangheem/models/aya_model.dart';
import 'package:tangheem/models/aya_tangheem_property.dart';
import 'package:tangheem/models/aya_tangheem_type.dart';
import 'package:tangheem/models/aya_tangheem_type_mapped.dart';
import 'package:tangheem/models/content_info_model.dart';
import 'package:tangheem/models/country_model.dart';
import 'package:tangheem/models/discussion_model.dart';
import 'package:tangheem/models/general_response_model.dart';
import 'package:tangheem/models/member_model.dart';
import 'package:tangheem/models/navigation_model.dart';
import 'package:tangheem/models/quick_links_model.dart';
import 'package:tangheem/models/surah_model.dart';
import 'package:tangheem/models/tangheem_type_model.dart';
import 'package:tangheem/models/welcome_video_model.dart';

import 'api_client.dart';

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

  TangheemUserApiClient._internal();

  factory TangheemUserApiClient() => _instance;

  Future<SurahModel> getSurahs() async {
    String url = "${ApiConsts.tangheemUsers}AlSuar_Get";
    var postParams = {};
    return await ApiClient().postJsonForObject((json) => SurahModel.fromJson(json), url, postParams);
  }

  Future<AyatTangheemTypeMapped> getTangheemBySurah(int surahId, int numberInSurahs) async {
    String url = "${ApiConsts.tangheemUsers}AyatTangheemTypeMapped_Get";
    var postParams = {"surahNo": surahId, "numberInSurahs": numberInSurahs.toString()};
    return await ApiClient().postJsonForObject((json) => AyatTangheemTypeMapped.fromJson(json), url, postParams);
  }

  Future<AyaModel> getAyaByFilter(int surahID, int ayahFrom, int ayahTo) async {
    String url = "${ApiConsts.tangheemUsers}AyatByRange_Get";
    var postParams = {"surahID": surahID, "ayahFrom": ayahFrom, "ayahTo": ayahTo};
    return await ApiClient().postJsonForObject((json) => AyaModel.fromJson(json), url, postParams);
  }

  Future<AyaModel> getQuranByPageNo(int pageNo) async {
    String url = "${ApiConsts.tangheemUsers}Quran_Get";
    var postParams = {"pageNo": pageNo};
    return await ApiClient().postJsonForObject((json) => AyaModel.fromJson(json), url, postParams);
  }

  Future<AyaModel> getAyaByRange(int itemsPerPage, int currentPageNo, int surahID, int ayahFrom, int ayahTo) async {
    String url = "${ApiConsts.tangheemUsers}AyatByRange_Get";
    var postParams = {"itemsPerPage": itemsPerPage, "currentPageNo": currentPageNo, "surahID": surahID, "ayahFrom": ayahFrom, "ayahTo": ayahTo};
    return await ApiClient().postJsonForObject((json) => AyaModel.fromJson(json), url, postParams);
  }

  Future<TangheemType> getTangheemType() async {
    String url = "${ApiConsts.tangheemUsers}TangheemType_Get";
    var postParams = {};
    return await ApiClient().postJsonForObject((json) => TangheemType.fromJson(json), url, postParams);
  }

  Future<AyatTangheemTypeMapped> getAyaTangheemTypeMappedRelated(int surahNo, String ayatNumberInSurahs, {String tangheemTypeId}) async {
    String url = "${ApiConsts.tangheemUsers}AyatTangheemTypeMapped_Get";
    var postParams = {"surahNo": surahNo, "numberInSurahs": ayatNumberInSurahs};
    if (tangheemTypeId != null) {
      postParams["tangheemTypeId"] = tangheemTypeId;
    }
    return await ApiClient().postJsonForObject((json) => AyatTangheemTypeMapped.fromJson(json), url, postParams);
  }

  Future<AyatTangheemTypeMapped> getAyaTangheemTypeMapped(int surahNo, String tangheemTypeName, String ayaText, int itemsPerPage, int currentPageNo) async {
    String url = "${ApiConsts.tangheemUsers}AyatTangheemTypeMapped_Get";
    var postParams = {};
    if (surahNo != null) {
      postParams["surahNo"] = surahNo;
    }
    if (tangheemTypeName != null) {
      postParams["tangheemTypeName"] = tangheemTypeName;
    }
    if (ayaText != null) {
      postParams["ayaText"] = ayaText;
    }
    postParams["itemsPerPage"] = itemsPerPage;
    postParams["currentPageNo"] = currentPageNo;

    return await ApiClient().postJsonForObject((json) => AyatTangheemTypeMapped.fromJson(json), url, postParams);
  }

  Future<AyatTangheemTypeMapped> getAyaTangheemTypeMappedFor(int surahNo, String numberInSurahs, String tangheemTypeId) async {
    String url = "${ApiConsts.tangheemUsers}AyatTangheemTypeMapped_Get";
    var postParams = {"surahNo": surahNo, "numberInSurahs": numberInSurahs, "tangheemTypeId": tangheemTypeId};
    return await ApiClient().postJsonForObject((json) => AyatTangheemTypeMapped.fromJson(json), url, postParams);
  }

  Future<AyatTangheemTypeMapped> ayahBaseTextGet(String tangheemTypeName, String ayaText, int itemsPerPage, int currentPageNo) async {
    String url = "${ApiConsts.tangheemUsers}AyahBaseText_Get";
    var postParams = {};
    if (tangheemTypeName != null) {
      postParams["tangheemTypeName"] = tangheemTypeName;
    }
    if (ayaText != null) {
      postParams["ayahTextBase"] = ayaText;
    }
    postParams["itemsPerPage"] = itemsPerPage;
    postParams["currentPageNo"] = currentPageNo;

    return await ApiClient().postJsonForObject((json) => AyatTangheemTypeMapped.fromJson(json), url, postParams);
  }

  Future<AyaTangheemType> getAyaTangheemType(int surahNo, String tangheemTypeName) async {
    String url = "${ApiConsts.tangheemUsers}AyaTangheemType_Get";

    var postParams = {};
    if (surahNo != null) {
      postParams["surahNo"] = surahNo;
    }
    if (tangheemTypeName != null) {
      postParams["tangheemTypeName"] = tangheemTypeName;
    }

    return await ApiClient().postJsonForObject((json) => AyaTangheemType.fromJson(json), url, postParams);
  }

  Future<AyaTangheemProperty> getAyaTangheemProperty(int currentPage, int surahNo, String tangheemTypeName) async {
    String url = "${ApiConsts.tangheemUsers}AyaTangheemProperty_Get";
    var postParams = {"itemsPerPage": 5, "currentPageNo": currentPage, "surahNo": surahNo, "tangheemTypeName": tangheemTypeName};
    return await ApiClient().postJsonForObject((json) => AyaTangheemProperty.fromJson(json), url, postParams);
  }

  Future<QuickLinksModel> quickLinks() async {
    String url = "${ApiConsts.tangheemUsers}QuickLinks_Get";
    var postParams = {};
    return await ApiClient().postJsonForObject((json) => QuickLinksModel.fromJson(json), url, postParams);
  }

  Future<GeneralResponseModel> contactUs(String firstName, String lastName, String email, String phone, String description) async {
    String url = "${ApiConsts.tangheemUsers}ContactUs_Add";
    var postParams = {"firstName": firstName, "lastName": lastName, "email": email, "phone": phone, "description": description};
    return await ApiClient().postJsonForObject((json) => GeneralResponseModel.fromJson(json), url, postParams);
  }

  Future<CountryModel> getCountry() async {
    String url = "${ApiConsts.tangheemUsers}Country_Get";
    var postParams = {};
    return await ApiClient().postJsonForObject((json) => CountryModel.fromJson(json), url, postParams);
  }

  Future<DiscussionModel> getDiscussionByTangheemID(int pageNumber, String tangheemID) async {
    String url = "${ApiConsts.tangheemUsers}Discussion_Get";
    var postParams = {"itemsPerPage": 5, "statusId": 2, "ayaTangheemTypeId": tangheemID};
    return await ApiClient().postJsonForObject((json) => DiscussionModel.fromJson(json), url, postParams);
  }

  Future<GeneralResponseModel> addStatistics(int statisticsTypeId) async {
    String url = "${ApiConsts.tangheemUsers}Statistics_Add";
    var postParams = {"statisticsTypeId": statisticsTypeId};
    return await ApiClient().postJsonForObject((json) => GeneralResponseModel.fromJson(json), url, postParams);
  }

  Future<NavigationModel> getNavigation() async {
    String url = "${ApiConsts.tangheemUsers}Navigation_Get";
    var postParams = {};
    return await ApiClient().postJsonForObject((json) => NavigationModel.fromJson(json), url, postParams);
  }

  Future<MemberModel> getMembers() async {
    String url = "${ApiConsts.tangheemUsers}Committee_Get";
    var postParams = {};
    return await ApiClient().postJsonForObject((json) => MemberModel.fromJson(json), url, postParams);
  }

  Future<ContentInfoModel> getContentInfo(int contentId) async {
    String url = "${ApiConsts.tangheemUsers}ContentInfo_Get";
    var postParams = {"contentTypeId": contentId};
    return await ApiClient().postJsonForObject((json) => ContentInfoModel.fromJson(json), url, postParams);
  }

  Future<String> getWelcomeVideo() async {
    String url = "${ApiConsts.tangheemUsers}WelcomeVideoUrl_Get";
    return await ApiClient().getJsonForObject((json) => WelcomeVideoModel.fromJson(json).welcomeVideoUrl, url);
  }
}
