import { Component, OnInit, ViewChild } from '@angular/core';
import { LocalStorage } from "../core/services/local_storage.service"
import { HyperService } from '../core/services/http.service';
import { Router, NavigationEnd, RoutesRecognized } from "@angular/router";
import * as moment from 'moment';
import { Local } from 'protractor/built/driverProviders';
import { filter, pairwise } from 'rxjs/operators';
declare var $:any;
@Component({
  selector: 'app-launch-threesixty-dashboard',
  templateUrl: './launch-threesixty-dashboard.component.html',
  styleUrls: ['./launch-threesixty-dashboard.component.scss']

})
export class LaunchThreesixtyDashboardComponent implements OnInit {
  imageSources = []; //Slider image array
  user_type: any; //User Am or DSE
 user_role:any;
 activeBanner:string;
 model_id:any;
 model_name:string='';
 loader:boolean=false;
 previousUrl: string;
  constructor(private server: HyperService, private router: Router) {

  }

  ngOnInit() {
    this.router.events
      .pipe(filter((e: any) => e instanceof RoutesRecognized),
        pairwise()
      ).subscribe((e: any) => {
        console.log(e[0].urlAfterRedirects);
        this.previousUrl = e[0].urlAfterRedirects// previous url
      });
      LocalStorage.setValue('is_firsttime', false);
      this.user_role=LocalStorage.getValue('user_data').role ;
      if (LocalStorage.getValue('user_data').role != 7 && LocalStorage.getValue('user_data').role > 7 && LocalStorage.getValue('user_data').role != 14) {
          this.user_type = 0; //Sales Ex
      } else if (LocalStorage.getValue('user_data') != undefined &&( LocalStorage.getValue('user_data').role <= 7 || LocalStorage.getValue('user_data').role ==14)) {

          this.user_type = 1; //Manager
      }
    //   console.log(this.user_type,LocalStorage.getValue('user_data').role)
      this.getBanner(); //get banner details


  }

  getBanner() {
    this.loader=true;
      this.server.get("explore/model")
          .then((data) => {
              if (data.status == 200 && data.result.response != 'failed' && data.result.data != null) {
                this.loader=false;
                this.imageSources=data.result.data;
                setTimeout(() => {
                  this.activeBanner=LocalStorage.getValue("set_model_id" );
                  if(LocalStorage.getValue("from_menu" )==true){
                     this.activeBanner=this.imageSources[0].model_id;
                     LocalStorage.setValue("set_model_id",  this.imageSources[0].model_id )
                  }else{
                     this.activeBanner=LocalStorage.getValue("set_model_id" );
                  }
                  console.log(this.activeBanner,LocalStorage.getValue("set_model_id" ))
                }, 100);

                console.log(this.activeBanner,LocalStorage.getValue("set_model_id" ))
                  for (let i = 0; i < this.imageSources.length; i++) {
                      if(this.activeBanner==this.imageSources[i].model_id){
                        this.activeBanner = this.imageSources[i].model_id;
                      }

                  }
                  // this.activeBanner ==1
                  // if(this.router.url=="url /threesixty-dashboard"){
                  //   this.activeBanner = this.imageSources[0].model_id;
                    // LocalStorage.setValue("set_model_id",  this.imageSources[0].model_id );
                    // LocalStorage.setValue("temp_set_model_id",  this.imageSources[0].model_id );
                  // }else if(this.previousUrl!="url /threesixty-dashboard"){

                  //   this.activeBanner=LocalStorage.getValue("set_model_id" );
                  // }else{
                  //   this.activeBanner =1
                  // }
                  // LocalStorage.setValue("set_model_id",  this.imageSources[0].model_id );
                   setTimeout(() => {
                   this.model_name=this.imageSources[0].model_name;
                }, 100);

              } else if (data.result.status == 401 && data.result.response == 'failed') {
                  this.router.navigateByUrl('');
              } else {
                  this.imageSources = [];
                  this.loader=false;
                  alert(data.result.message)
              }
          });
  }

  explore() {
    LocalStorage.setValue("from_menu", false);
    setTimeout(() => {
      this.model_id = $('.carousel-inner').find('.active').text();
      let temp_model = $.trim(this.model_id);
      for (let i = 0; i < this.imageSources.length; i++) {
        if (temp_model == this.imageSources[i].model_id) {
          this.model_name = this.imageSources[i].model_name;
          LocalStorage.setValue("model_name", this.model_name);
          LocalStorage.setValue("set_model_id", temp_model);
        }
      }
      this.router.navigateByUrl('/launch-threesixty')
    }, 1000);
  }
  getModelName() {
    setTimeout(() => {
      this.model_id = $('.carousel-inner').find('.active').text();
      console.log(this.model_id)
      let temp_model = $.trim(this.model_id);
      for (let i = 0; i < this.imageSources.length; i++) {
        if (temp_model == this.imageSources[i].model_id) {
          this.model_name = this.imageSources[i].model_name;
          LocalStorage.setValue("model_name", this.model_name);
          LocalStorage.setValue("set_model_id", temp_model);
        }
      }
    }, 1000);
  }
  goToPrev(){

  }
}
