import { Component, OnInit, ViewChild } from '@angular/core';
import { BaseComponent } from '../../base/base.component';
import { ActivatedRoute, Router } from '@angular/router';
import { takeUntil } from 'rxjs/operators';
import { PersonalDetailsComponent } from '../../mc-shared/personal-details/personal-details.component';
import { WorkHistoryComponent } from './work-history/work-history.component';
import { EducationComponent } from './education/education.component';
import { SkillsComponent } from './skills/skills.component';
import { ReferencesComponent } from './references/references.component';

@Component({
    selector: 'app-career-history',
    templateUrl: './career-history.component.html',
    styleUrls: ['./career-history.component.scss']
})
export class CareerHistoryComponent extends BaseComponent implements OnInit {

    tabs = ['templates', 'personal-data', 'work-history', 'education', 'skills', 'references', 'publish'];
    activeTab = '';
    isExiting = false;

    @ViewChild('personalDetailsComponent') personalDetailsComponent: PersonalDetailsComponent;
    @ViewChild('workHistoryComponent') workHistoryComponent: WorkHistoryComponent;
    @ViewChild('educationComponent') educationComponent: EducationComponent;
    @ViewChild('skillsComponent') skillsComponent: SkillsComponent;
    @ViewChild('referencesComponent') referencesComponent: ReferencesComponent;

    constructor(
        private router: Router,
        private activatedRoute: ActivatedRoute
    ) {
        super();
    }

    ngOnInit() {
        this.activatedRoute.params
            .pipe(takeUntil(this.componentInView))
            .subscribe(params => {
                if (params.screen) {
                    this.activeTab = params.screen;
                }
            })
    }

    exit(): void {
        switch (this.activeTab) {
            case 'personal-data':
                this.personalDetailsComponent.updateProfile('/my-career-web/dashboard/home');
                break;
            case 'work-history':
                this.workHistoryComponent.saveWorkHistory('/my-career-web/dashboard/home');
                break;
            case 'education':
                this.educationComponent.saveEducation('/my-career-web/dashboard/home');
                break;
            case 'skills':
                this.skillsComponent.saveSkill('/my-career-web/dashboard/home');
                break;
            case 'references':
                this.referencesComponent.saveReferees('/my-career-web/dashboard/home');
                break;
        }
    }

    tabChanged($event): void {
        this.router.navigate([`/my-career-web/career-history/${this.tabs[$event.index]}`]);
    }

}
