import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';

import { environment } from 'src/environments/environment';
import { HttpHeaderOptions } from 'src/app/dtos/http-header-option';
import { StateService } from '../state.service';
import { AuthenticatedUser } from 'src/app/dtos/authenticated-user.model';
import { OidcSecurityService } from 'angular-auth-oidc-client';


@Injectable()
export class AppApiService {
    Kommunenummer:any;

    httpOptions: HttpHeaderOptions = new HttpHeaderOptions();

    constructor(private http: HttpClient,
        private stateService: StateService,
        public oidcSecurityService: OidcSecurityService) {
        this.httpOptions.headers = new HttpHeaders({
            'Content-Type': 'application/json,image/jpg,image/png',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Credentials': 'true',
            'Authorization': "Bearer " + this.oidcSecurityService.getToken()
        });
        this.stateService.init();
    }

    createAuthorizationHeader(headers: HttpHeaders) {
        headers.set('Content-Type', 'application/json');
    }

    loadItems(path: string) {
        return new Promise<any>((resolve, reject) => {
            this.createAuthorizationHeader(this.httpOptions.headers);
            let urlPath = path +'?tenantId=' + this.stateService.getKommunenummer();
            this.http.get<any>(urlPath, { 'headers': this.httpOptions.headers })
                .subscribe((response: any) => {
                    resolve(response);
                },
                    err => {
                        reject(err);
                    },
                    () => {
                        // console.log("loaded")

                    })
                .add(() => {
                });

        });
    }

    loadItem(path: string) {
      return new Promise<any>((resolve, reject) => {
          this.createAuthorizationHeader(this.httpOptions.headers);
          let urlPath = path +'?tenantId=' + this.stateService.getKommunenummer();
          this.http.get<any>(urlPath, { 'headers': this.httpOptions.headers })
              .subscribe((response: any) => {
                  resolve(response);
              },
                  err => {
                      reject(err);
                  },
                  () => {
                      // console.log("loaded")
                  })
              .add(() => {
              });

      });
    }

    loadItemWithExtra(path: string, param) {
      return new Promise<any>((resolve, reject) => {
          this.createAuthorizationHeader(this.httpOptions.headers);
          let urlPath = path +'?tenantId=' + this.stateService.getKommunenummer() + param;
          this.http.get<any>(urlPath, { 'headers': this.httpOptions.headers })
              .subscribe((response: any) => {
                  resolve(response);
              },
                  err => {
                      reject(err);
                  },
                  () => {
                      // console.log("loaded")
                  })
              .add(() => {
              });

      });
    }

    saveItems(path: string, data: any) {
        return new Promise<any>((resolve, reject) => {
            this.createAuthorizationHeader(this.httpOptions.headers);
            let urlPath = path + '?tenantId=' + this.stateService.getKommunenummer();
            this.http.post<any>(urlPath, data, { 'headers': this.httpOptions.headers })
                .subscribe((response: any) => {
                    resolve(response);
                },
                    err => {
                        reject(err);
                    },
                    () => {
                        // console.log("loaded")
                    })
                .add(() => {
                });

        });
    }

    deleteItems(path: string) {
        return new Promise<any>((resolve, reject) => {
            this.createAuthorizationHeader(this.httpOptions.headers);
            let urlPath = path + '?tenantId=' + this.stateService.getKommunenummer();
            this.http.delete<any>(urlPath, { 'headers': this.httpOptions.headers })
                .subscribe((response: any) => {
                    resolve(response);
                },
                    err => {
                        reject(err);
                    },
                    () => {
                        // console.log("loaded")
                    })
                .add(() => {
                });

        });
    }

    patchItems(path: string, data: any) {
        return new Promise<any>((resolve, reject) => {
            this.createAuthorizationHeader(this.httpOptions.headers);
            let urlPath = path + '?tenantId=' + this.stateService.getKommunenummer();
            this.http.patch<any>(urlPath, data, { 'headers': this.httpOptions.headers })
                .subscribe((response: any) => {
                    resolve(response);
                },
                    err => {
                        reject(err);
                    },
                    () => {
                        // console.log("loaded")
                    })
                .add(() => {
                });

        });
    }


    putItems(path: string, data: any) {
      return new Promise<any>((resolve, reject) => {
          this.createAuthorizationHeader(this.httpOptions.headers);
          let urlPath = path + '?tenantId='+ this.stateService.getKommunenummer();
          this.http.put<any>(urlPath, data, { 'headers': this.httpOptions.headers })
              .subscribe((response: any) => {
                  resolve(response);
              },
                  err => {
                      reject(err);
                  },
                  () => {
                      // console.log("loaded")
                  })
              .add(() => {
              });

      });
    }
}
