import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Country } from '../../models/country';
import { Districts } from '../../models/districts';
import { Cities } from '../../models/cities';
import { States } from '../../models/state';

@Injectable({
    providedIn: 'root'
})
export class RegionService {

    constructor(private http: HttpClient) { }

    async getCountries(startIndex: number | null, fetchRecords: number | null, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        let parameter = JSON.parse(JSON.stringify({ "startIndex": startIndex, "fetchRecords": fetchRecords }))
        return await this.http.post("/api/customer/cities/getCountries", parameter, { headers: reqHeader })
            .toPromise() as any
    }

    async getStates(countryId: number, startIndex: number | null, fetchRecords: number | null, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        let parameter = JSON.parse(JSON.stringify({ "startIndex": startIndex, "fetchRecords": fetchRecords, "countryId": countryId }))
        return await this.http.post("/api/customer/cities/getStates", parameter, { headers: reqHeader })
            .toPromise() as any
    }

    async getDistricts(stateId: number, startIndex: number | null, fetchRecords: number | null, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        let parameter = JSON.parse(JSON.stringify({ "startIndex": startIndex, "fetchRecords": fetchRecords, "stateId": stateId }))
        return await this.http.post("/api/customer/cities/getDistricts", parameter, { headers: reqHeader })
            .toPromise() as any
    }

    async getCities(districtId: number, startIndex: number | null, fetchRecords: number | null, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        let parameter = JSON.parse(JSON.stringify({ "startIndex": startIndex, "fetchRecords": fetchRecords, "districtId": districtId }))
        return await this.http.post("/api/customer/cities/getCities", parameter, { headers: reqHeader })
            .toPromise() as any
    }

    async insertCountry(country: Country, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        return await this.http.post("/api/customer/cities/insertCountry", country, { headers: reqHeader })
            .toPromise() as any
    }

    async updateCountry(country: Country, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        return await this.http.post("/api/customer/cities/updateCountry", country, { headers: reqHeader })
            .toPromise() as any
    }

    async activeInactiveCountry(countryId: number, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        let parameter = JSON.parse(JSON.stringify({ "countryId": countryId }))
        return await this.http.post("/api/customer/cities/activeInactiveCountry", parameter, { headers: reqHeader })
            .toPromise() as any
    }

    async insertState(state: States, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        return await this.http.post("/api/customer/cities/insertState", state, { headers: reqHeader })
            .toPromise() as any
    }

    async updateState(state: States, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        return await this.http.post("/api/customer/cities/updateState", state, { headers: reqHeader })
            .toPromise() as any
    }

    async activeInactiveState(stateId: number, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        let parameter = JSON.parse(JSON.stringify({ "stateId": stateId }))
        return await this.http.post("/api/customer/cities/activeInactiveState", parameter, { headers: reqHeader })
            .toPromise() as any
    }

    async insertDistrict(district: Districts, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        return await this.http.post("/api/customer/cities/insertDistrict", district, { headers: reqHeader })
            .toPromise() as any
    }

    async updateDistrict(district: Districts, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        return await this.http.post("/api/customer/cities/updateDistrict", district, { headers: reqHeader })
            .toPromise() as any
    }

    async activeInactiveDistrict(districtId: number, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        let parameter = JSON.parse(JSON.stringify({ "districtId": districtId }))
        return await this.http.post("/api/customer/cities/activeInactiveDistrict", parameter, { headers: reqHeader })
            .toPromise() as any
    }

    async insertCity(city: Cities, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        return await this.http.post("/api/customer/cities/insertCity", city, { headers: reqHeader })
            .toPromise() as any
    }

    async updateCity(city: Cities, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        return await this.http.post("/api/customer/cities/updateCity", city, { headers: reqHeader })
            .toPromise() as any
    }

    async activeInactiveCity(cityId: number, token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        let parameter = JSON.parse(JSON.stringify({ "cityId": cityId }))
        return await this.http.post("/api/customer/cities/activeInactiveCity", parameter, { headers: reqHeader })
            .toPromise() as any
    }

    async getAllRegionData(token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        return await this.http.post("/api/customer/cities/getAllRegionData", {}, { headers: reqHeader })
            .toPromise() as any
    }

    async updateRegionData(data:any,token: string): Promise<any> {
        let reqHeader = new HttpHeaders({ "Authorization": token });
        let parameter = JSON.parse(JSON.stringify({ "data": data }));
        return await this.http.post("/api/customer/cities/updateRegionData", parameter, { headers: reqHeader })
            .toPromise() as any
    }

    
}