import  {Component, OnInit, OnDestroy} from '@angular/core';
import { MatDialog, MatDialogConfig} from '@angular/material/dialog';
import { FullnodeService } from '../fullnode.service';
import { Router } from '@angular/router';
import { UserService } from '../user.service';
import { CancelComponent } from '../cancel/cancel.component';
import { Observable } from 'rxjs';

import {Owner} from '../owner.model';

import { LanguageService } from '../language.service';

@Component({
	selector: 'app-header',
	templateUrl: './header.component.html',
	styleUrls: ['./header.component.css']
})

export class HeaderComponent implements OnInit, OnDestroy {

	public height = 0;
	private owner: Owner= {
		_id:'',
		address: 'none',
		name:'',
		currency: 'usd',
		tax: false,
		taxValue: 0,
		vat: false,
		vatValue: 0,
		paid: false,
		zats: false,
		invoices: false,
		expiration: new Date(Date.now()).toISOString(),
		payconf: false,
		crmToken: ''
	};
	public heightUpdate: Observable<number>;
	public ownerUpdate: Observable<Owner>;
	public uZaddrUpdate: Observable<string>;

    //
    // Language Support
    //
    vE = {
    	headerGetCurrency : '',
    	headerLastBlock : '',
    	headerLogoutLbl : '',
    	headerLogoutMsg1 : '',
    	headerLogoutMsg2 : ''
    }
    //
    // ------------------------------------------------------------
	constructor(
		public fullnodeService: FullnodeService,
		public userService: UserService,
		private dialog: MatDialog,
		private router: Router,
		private languageService: LanguageService
	){
		this.heightUpdate = fullnodeService.heightUpdate;
		this.uZaddrUpdate = userService.uZaddrUpdate;
		this.ownerUpdate = userService.ownerUpdate;
		this.ownerUpdate.subscribe((owner) => {
			this.owner = owner;
		});
	}

	ngOnInit(){
        this.chgUILanguage();
	}

	ngOnDestroy(){
	}

	getCurrency(){
		return this.owner.currency.toUpperCase();
	}

	logout(){
		const dialogConfig = new MatDialogConfig();

		dialogConfig.disableClose = true;
		dialogConfig.autoFocus = true;
		dialogConfig.data = { title: this.vE.headerLogoutLbl, 
		                      msg1: this.vE.headerLogoutMsg1, 
		                      msg2: this.vE.headerLogoutMsg2 };

		//console.log(dialogConfig.data);		                      

		const dialogRef = this.dialog.open(CancelComponent, dialogConfig);
		dialogRef.afterClosed().subscribe(val => {
			if(val){
//				console.log('Logout!');
				this.userService.deleteUser().subscribe(UserResponse => {
					//console.log('Rerouting');
					this.userService.findUser();
					this.router.navigate(['/login']);
				});
			}
		});
	}

	chgUILanguage(){
        //console.log('HEADER.chgUILanguage Called ');
 		this.languageService.getViewElements('header').subscribe({
			next: response => { 
					//console.log('Received >> ', response );
					//console.log('Language Code : ', response.language);
					//console.log('Component Name : ',response.component);
					//console.log('Language data : ',response.data);

			        this.vE.headerLastBlock = response.data.header_last_block;
			        this.vE.headerGetCurrency = response.data.header_get_currency;
			        this.vE.headerLogoutLbl = response.data.header_logout_lbl;
			        this.vE.headerLogoutMsg1 = response.data.header_logout_msg1;
			        this.vE.headerLogoutMsg2 = response.data.header_logout_msg2;

			    },
			error: error  => { console.log('Error >> ',error); }
		});
	}

}
