import { Component, Input, OnInit } from '@angular/core';
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { DocumentConfig} from 'src/app/interfaces/common-interfaces';
import { UtilitiesService } from 'src/app/services/utilities/utilities.service';
import { Subscription } from 'rxjs';
import { formatDate } from '@angular/common';

import { AuthorizedStatesService } from 'src/app/services/authorizedStates/authorized-states.service';

@Component({
  standalone: false,
    selector: 'app-auth-state-license-upload-modal',
    templateUrl: './auth-state-license-upload-modal.component.html',
    styleUrls: ['./auth-state-license-upload-modal.component.scss']
})
export class AuthStateLicenseUploadModalComponent implements OnInit {

    @Input() licenseUploadClickOk!: () => void;
    @Input() licenseUploadModalConfig!: any;
    @Input() stateName!: string;
    @Input() authType!: string;
    @Input() stateCode!: string;
    @Input() licenseExpirationDate: any = "0000-00-00";;
    @Input() setLicenseExpire!: () => void;
    @Input() isAdmin!: boolean;
    @Input() showStatusMsg!: boolean;
    @Input() statusMsg!: string;
    @Input() step1!: string;
    @Input() step2!: string;
    @Input() step3!: string;
    @Input() AuthStateLicenseUploadModal!:NgbModalRef;
    showExpireError: boolean = false;
    expireErrorMsg: string = "";
    modifiedExpDate: any = "0000-00-00";

    constructor(private utilities: UtilitiesService, private authorizedStatesService : AuthorizedStatesService) { }

    ngOnInit(): void {
        this.authStateLoadEventListener();
    }

    today: Date = new Date();
    /**
    * Upload-document directive related variables
    */
    stateLicenseDocConfig: DocumentConfig = {
        param: 'auth_state_license_document',
        buttonName: 'Upload License',
        className: 'col-xs-6 col-sm-6 col-md-6 col-lg-6',
        uploadBtnClass: 'col-lg-12 col-md-10',
        buttonClass: 'upload-btn-cls',
        error: false,
        errorMsg: '',
        accFileTypes: []
    };

    /**
     * Update document config with the object sent by upload document directive
     * after the upload document process is complete.
     * @param docConfig 
     */
    updateDocConfig(docConfig: DocumentConfig) {
       this.utilities.merge(this.stateLicenseDocConfig, docConfig);
    }

    dateFormat(date: string | Date, format: string = 'yyyy-MM-dd') {
       return date != "" ? formatDate(date, format, 'en') : date;
    }


    /**
     * Method to update license expiration date
     * @param string expirationDate
     * @param string stateCode
     * @param string authType
     * @returns void
     */
    updateLicenseExpSubscribe!: Subscription;
    updateLicenseExpiration(expirationDate: string, stateCode: string, authType: string) {
            this.showExpireError = false;
            var documentExpire = "0000-00-00";
            if (expirationDate !== null) {
                var documentExpire = this.dateFormat(expirationDate, 'yyyy-MM-dd');
            }
            if (this.step2 !== 'rsh-complete'){
            this.step1 = 'rsh-complete';
            this.step2 = 'rsh-active';
            }else {
            this.step1 = 'rsh-complete';
            };
            this.modifiedExpDate = documentExpire;
    };

    /**
    * Catch the file event raised by the document directive
    * and use the data to display messages and switch content.
    * @returns void
    */
    authStateLoadEventListener () {
        this.utilities.authStateLoadEvent.subscribe((data: any) => {
            if (data.config.uploadStatus === false && data.config.errorMsg === "") {
                this.step2 = 'rsh-complete';
                this.step3 = 'rsh-active';
            }
            if (data.config.errorMsg !== "") {
                this.showExpireError = true;
                this.expireErrorMsg = data.config.errorMsg;
            }
            if (data.config.uploadStatus === true) {
                var updateData = {
                    'state_code'  : this.stateCode,
                    'auth_type' : this.authType,
                    'expiration_date' : this.modifiedExpDate
                };
                this.updateLicenseExpSubscribe = this.authorizedStatesService.updateLicenseExpiration(updateData)
                .subscribe({
                next: () => {
                    this.showExpireError = false;
                    this.expireErrorMsg = "";
                },
                error: (err) => {
                    this.showExpireError = true;
                    this.expireErrorMsg = "Problem occured while updating the License Expiration Date";
                    this.authorizedStatesService.errorCallBack(err);
                },
                complete: () => { },
                });
                this.licenseUploadClickOk(); 
            }
        });
    }
}
