import {Component, OnInit} from '@angular/core';
import {NavigationExtras, Router} from '@angular/router';
import {TranslateService} from '@ngx-translate/core';
import {MENUS} from '../menu.const';
import {HomeService} from '../_service/home.service';
import {ProfileSerivce} from '../../profile/_service/profile.service';
import {BaseComponent} from '../../share/base.component';
import {MdSnackBar} from '@angular/material';
import {MdDialog} from '@angular/material';
import {TestEventService} from '../_service/testEvent.service';

@Component({
  selector: 'app-home',
  templateUrl: '../_views/home.component.html',
  styleUrls: ['../_views/home.component.scss']
})
export class HomeComponent extends BaseComponent implements OnInit {
  menus: any;
  user: any;
  selectedMenu: string;
  selectedLanguage: string = "en";

  constructor(public router: Router,
              private translate: TranslateService,
              private homeService: HomeService,
              private profileSerivce: ProfileSerivce,
              public dialog: MdDialog,
              private testEventService: TestEventService,
              public mdSnackBar: MdSnackBar) {
    super(router, mdSnackBar);
    this.menus = MENUS;
  }

  ngOnInit() {
    console.log("home component inti");
    this.user = {};
    this.loadUserInfo();
    let url = this.router.url;
    let comp = url.split("/");
    if(comp.length>2){
      switch (comp[2]) {
        case 'atlas':
        this.changeMenu('MenuAtlas');
          break;
        case 'testingtool':
        this.changeMenu('MenuTesting');
          break;
        default:
        this.changeMenu('MenuStart');
          break;
      }
    }
  }

  goHome() {
    this.selectedMenu = "Menu1";
    this.router.navigate(['/home']).then(nav => {
    }, err => {
      alert(err.message) // when there's an error
    });
  }

  changeMenu(cMenu) {
    this.selectedMenu = cMenu;
  }

  logout() {
    this.homeService.requestLogout().subscribe(res => {
      localStorage.clear();
      if (res.status === 'OK') {
        this.router.navigate(['/authen/login']);
      } else {
        console.error('Server error');
      }
    });
  }

  loadUserInfo() {
    const userStr: string = localStorage.getItem('userInfo');
    if (userStr) {
      this.user = JSON.parse(userStr);
    } else {
      this.profileSerivce.getUserCurrent().subscribe(res => {
        if (res.status === 'OK') {
          this.user = res.response;
          localStorage.setItem('userInfo', JSON.stringify(this.user));
        } else {
          this.processStatusError(res.errors);
          console.error('Server error');
        }
      });
    }
  }

  createTest(url, type) {
    if(type==undefined || type==null)
      type = 'testingtool';
    if(url==undefined || url==null)
      url = '/home/testingtool/testedit';
    url = '/home/' + type + '/testedit';
    this.homeService.requestCreateTest(null, type).subscribe(res => {
      if (res.status === 'OK') {
        this.testEventService.notify('OK');
        this.openSnackBar('Created test successful!');
        const navigationExtras: NavigationExtras = {
          queryParams: {
            id: res.response.TestId
          }
        };
        this.router.navigate([url], navigationExtras);
      } else {
        console.error('Server error');
        this.processStatusError(res.errors);
      }
    });
  }

  changeLang(lang: string) {
    this.translate.use(lang);
  }
}
