// Angular Core
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Location } from '@angular/common';

// Services
import { ApiService } from '../api/api.service';
import { LocalStorageService } from '../localstorage/local-storage.service';

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

  constructor(
    public api: ApiService, 
    private router: Router, 
    private location:Location,
    private localstorageService: LocalStorageService) { }

  registrationPages = [
    'storeInfo',
    'questionnaire',
    'eSign',
    'licenseVerification',
    'accountVerify'
  ];
  adminSuppPages = [
    'mainSettings',
    'shippingSettings',
    'manageCredits'
  ];
  pharmacySupplierUrl = [
    '/market/manageAccount',
    '/market/confirmEmail'
  ];
  confirmEmailPage = 'confirmEmail';

  /**
  * Get Account status
  *  
  */
  checkForPageAuthorization(userInfo: any, pageName: string) {
    if (userInfo.reg_step <= 7) {
      //redirect to specific url based on the registration step the user is in
      if (userInfo.reg_step === 1) {
        this.localstorageService.setItem('redirectedPage', "confirmEmail");
        this.router.navigate(['/market/confirmEmail']);
      }
      if (userInfo.reg_step === 5) {
        this.localstorageService.setItem('redirectedPage', "payment");
        this.router.navigate(['/market/payment']);
      }
      if (userInfo.reg_step === 6) {
        this.localstorageService.setItem('redirectedPage', "licenseVerification");
        this.router.navigate(['/market/licenseVerification']);
      }
      if (userInfo.reg_step === 7) {
        this.localstorageService.setItem('redirectedPage', "accountVerify");
        this.router.navigate(['/market/accountVerify']);
      }
      if (userInfo.reg_step >= 2 && pageName === 'storeInfo') {
        this.localstorageService.setItem('redirectedPage', pageName);
        this.router.navigate(['/market/storeInfo']);
      } else if (userInfo.reg_step <= 2 && pageName != 'storeInfo') {
        this.router.navigate(['market/storeInfo'])
      }
      if (userInfo.reg_step >= 3 && pageName === 'questionnaire' && userInfo.is_buyer === true) {
        this.localstorageService.setItem('redirectedPage', pageName);
        this.router.navigate(['/market/questionnaire']);
      } else if (userInfo.reg_step <= 3 && !['storeInfo', 'questionnaire'].includes(pageName) && userInfo.is_buyer === true) {
        this.router.navigate(['market/questionnaire'])
      }
      if (userInfo.reg_step >= 4 && pageName === 'eSign' && userInfo.is_buyer === true) {
        this.localstorageService.setItem('redirectedPage', pageName);
        this.router.navigate(['/market/eSign']);
      } else if (userInfo.reg_step <= 4 && !['storeInfo', 'questionnaire', 'eSign'].includes(pageName) && userInfo.is_buyer === true) {
        this.router.navigate(['market/eSign'])
      }
    }
    // Preview mode new user
    const isPreviewNewUser = userInfo.is_preview_mode && !userInfo.is_pms_registration_initiated;
    if (userInfo.reg_step > 7 && userInfo.is_email_confirmed === 0 && !isPreviewNewUser) {
      this.localstorageService.setItem('redirectedPage', "confirmEmail");
      this.router.navigate(['/market/confirmEmail']);
    }
    if (userInfo.reg_step > 7 && userInfo.is_email_confirmed === 1 &&
      (pageName === this.confirmEmailPage || this.registrationPages.indexOf(pageName) !== -1)) {
      if (userInfo.is_buyer === true) {
        this.localstorageService.setItem('redirectedPage', "home");
        this.router.navigate(['/market/home']);
      }
      if (userInfo.is_supplier === true) {
        this.localstorageService.setItem('redirectedPage', "home");
        this.router.navigate(['/market/supplier/home']);
      }
    }
    if (userInfo.reg_step > 7
      && userInfo.is_supplier
      && !userInfo.is_admin
      && this.adminSuppPages.indexOf(pageName) !== -1) {
        this.localstorageService.setItem('redirectedPage', "home");
        this.router.navigate(['/market/supplier/home']);
    }
    if (userInfo.reg_step > 7
      && userInfo.is_supplier
      && this.location.path().indexOf('market/supplier/') === -1
      && this.pharmacySupplierUrl.indexOf(this.location.path()) === -1
    ) {
      this.localstorageService.setItem('redirectedPage', "home");
      this.router.navigate(['/market/supplier/home']);
    }
    if (userInfo.reg_step > 7
      && !userInfo.is_supplier
      && this.location.path().indexOf('market/supplier/') > -1
      && this.pharmacySupplierUrl.indexOf(this.location.path()) === -1
    ) {
      this.localstorageService.setItem('redirectedPage', "home");
      this.router.navigate(['/market/home']);
    }
    if (userInfo.is_email_confirmed === 0 && !isPreviewNewUser) {
      this.localstorageService.setItem('redirectedPage', "confirmEmail");
      this.router.navigate(['/market/confirmEmail']);
    }
  };

}