import { environment } from './../environments/environment';
import { Component, Inject } from '@angular/core';
import { RouterLinkActive } from '@angular/router';
import { Title } from '@angular/platform-browser';
import {ContactService} from './contact.service';
import{Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { TranslateService } from 'ng2-translate';
import { LoginComponent } from './login/login.component';
import { DOCUMENT } from '@angular/common';



@Injectable()
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [LoginComponent],
  
})
export class AppComponent {
userData:any;
url:any;

logo:any;
text:any;
address:any;
mobile:any;
dev_url = environment.hostUrl;
showMenu = environment.showLandingpageMenu;
headerfooterPadding:any;
margintop:any;
  showNav: boolean;
constructor(private titleService: Title,private contactService: ContactService,private http: Http,public translate: TranslateService,public loginComp : LoginComponent){
  console.log("showMenu=>",this.showMenu);
  let split1 = document.URL.split('//')[1];
  let split2 = split1.split('/')[0];
  let splitForStyle = split1.split('/')[1];
  this.url =  split2;


  // let split3 = document.URL.split('referrer_token')[1]  || localStorage.getItem('referrer_token');
 

  var decode_token = function(token){
    var decodeToken = function(token){
      return token ? (window.atob(token)):token;
    }
    var parseToken = function(token){
   if(token && typeof(token)=='string'){
   try {
   return JSON.parse(token)
   } catch (e) {
   return "";
   }
   }
   else
   return "";
    }
    var getData = function(token){
   return token ? {
   "referer": decodeToken(token._referrer),
   "username": decodeToken(token._u),
   "pass": decodeToken(token._p)
   } : null;
    }
    return getData(parseToken(decodeToken(token)));
   }

let token = document.URL.split('referrer_token=')[1]  || localStorage.getItem('referrer_token=');
console.log(token);
  var split3 = decode_token(token) ; 
  console.log(split3);
  if(split3 && split3.referer){
    // var referrer_token = split3.substring(1);
    // var referer_data = window.atob(referrer_token).split(",");
    // var referer = referer_data[0].split("=")[1];
    // var username = referer_data[1].split("=")[1];
    // var password = referer_data[2].split("=")[1];
    // console.log(referer);
    // console.log(username);
    // console.log(password);
    // var r1 = referer.split('"')[0];
    // var u1 =  username.split('"')[0];
    // var p1 = password.split('"')[0];
    // console.log(r1);
    // var reffere_obj = {
    //   user : username,
    //   pass : password
    // }
    localStorage.setItem('referer_obj',JSON.stringify(split3));
    localStorage.setItem('referrer_token=',token);
    var referer  = split3['referer'];
    this.contactService.getReferenceId(referer).subscribe(res=>{
      // localStorage.setItem('referrer_token',split3);
        this.loginComp.loginemail() ;      
    },err=>{
      console.log(err);
    })

  }else{
 
    for(var i = 0; i <this.showMenu.length;i++){
      if (this.url == this.showMenu[i]) {
        this.showNav = true;
        return;
      } else {
        this.showNav = false;
      }
    }

    this.contactService.dealerInfo(this.url).subscribe(
     data => {
       this.userData = data 
       
       this.logo = this.dev_url+this.userData.logo;
       this.titleService.setTitle(this.userData.organisationName);
       this.text=this.userData.text;
       this.address = this.userData.address; 
       console.log("this.userData.email",this.userData.email);
       // this.dealerId  = 
       console.log("this.userData",this.userData);
       window.localStorage['dealerName']= this.userData.dealerName;
       window.localStorage['DealerID'] = this.userData.email;
         window.localStorage['DealerPhone'] =this.userData.contactNumber;
         window.localStorage['logo'] =this.logo;
         window.localStorage['address'] =this.userData.address;
         window.localStorage['text'] = this.text;
         window.localStorage['mobile'] = this.userData.contactNumber;
         window.localStorage['facebook'] = this.userData.facebook;
         window.localStorage['android'] = this.userData.androidApp;
         window.localStorage['iphone'] = this.userData.appleApp;
         window.localStorage['organisationName'] = this.userData.organisationName;
      if(window.localStorage[this.userData.dealerName]){
        console.log("Dealer Name =>", this.userData.dealerName);
      }
     }) 
 
     // translate.setDefaultLang('en');
     // translate.use('en');
  
  }

};


color:any;
fun1(value){

  if(value=="signup"){
    this.color='red';
    // console.log(this.color);
  }
}
ngOnDestroy() {
 window.localStorage.clear();
}


}
