import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { AuthService } from 'src/app/auth.service';
import { environment } from '../../../environments/environment';
@Component({
  selector: 'app-workarea',
  templateUrl: './workarea.component.html',
  styleUrls: ['./workarea.component.css']
})
export class WorkareaComponent implements OnInit {
  url = environment.commontoolappUrl;
  userid: string = localStorage.getItem('userid');
  urlSafe: SafeResourceUrl;
  projectid: number;
  jtid: number;
  projecttransid: number;
  jbid: number;
  jobtitle: string;
  spath: string;
  maintranid: number;
  constructor(
    public sanitizer: DomSanitizer,
    private route: ActivatedRoute,
    public auth: AuthService,
  ) {
  }
 /* ngOnInit(): void {
    this.projectid = +this.route.snapshot.queryParamMap.get('projectid');
    this.jtid = +this.route.snapshot.queryParamMap.get('jtid');
    this.spath = this.route.snapshot.queryParamMap.get('spath');
    if (this.route.snapshot.queryParamMap.get('spath') !== 'editortool') {
      if (this.route.snapshot.queryParamMap.get('spath') !== null
        && this.route.snapshot.queryParamMap.get('spath') !== 'CMApprovalTool') {
        this.url = this.route.snapshot.queryParamMap.get('spath');
      } else if (this.route.snapshot.queryParamMap.get('spath') === 'CMApprovalTool') {
        this.url = environment.cmapprovalappUrl;
      } else {
        this.url = environment.commontoolappUrl;
      }
    } else {
      this.maintranid = +this.route.snapshot.queryParamMap.get('maintranid');
      this.url = environment.editorappUrl;
    }//
    const jwt_token = this.auth.getSecureToken();
    const enviroment = environment.environment;
    if (this.spath !== 'editortool') {
      this.urlSafe = this.sanitizer.bypassSecurityTrustResourceUrl(this.url + '/' + jwt_token + '/' + this.userid
        + '/' + this.jtid + '/' + this.projectid + '/' + enviroment);
    } else {
      this.urlSafe = this.sanitizer.bypassSecurityTrustResourceUrl(this.url + '/' + jwt_token + '/' + this.userid
        + '/' + this.jtid + '/' + this.projectid + '/' + enviroment + '*' + this.maintranid);
    }
  }*/


  ngOnInit(): void {
  
    this.projectid = +this.route.snapshot.queryParamMap.get('projectid');
  
      this.jtid = +this.route.snapshot.queryParamMap.get('jtid');
  
      if (this.route.snapshot.queryParamMap.get('spath') !== null) {
  
       this.url = this.route.snapshot.queryParamMap.get('spath');
      //  this.url = environment.commontoolappUrl;
    
    } else  {
    
      this.url = environment.commontoolappUrl;
    
    }
     
   const jwt_token = this.auth.getSecureToken();
   
     const enviroment = environment.environment;
     if(this.url.includes('xmleditor')) {
      let jsonString = '{\"inpath\":"localpath", \"outpath\":"outpath", \"returnurl\":"urltoreturn"}'; 
      this.urlSafe = this.sanitizer.bypassSecurityTrustResourceUrl(this.url + '/' + jwt_token + '/' + this.userid
      + '/' + this.jtid + '/' + this.projectid + '/' + enviroment + '/' + encodeURIComponent(jsonString));
      
     } else {
     // this.urlSafe = this.sanitizer.bypassSecurityTrustResourceUrl(this.url + '/' + jwt_token + '/' + this.userid
     // + '/' + this.jtid + '/' + this.projectid + '/' + enviroment);
    /*  this.urlSafe = this.sanitizer.bypassSecurityTrustResourceUrl(
      `${this.url}/${jwt_token}/${this.userid}/${this.jtid}/${this.projectid}/${enviroment}`); */
      this.urlSafe = this.sanitizer.bypassSecurityTrustResourceUrl(this.url + '/' + jwt_token +  '/' +this.userid
        + '/' + this.jtid + '/' + this.projectid + '/' + enviroment);
    
     }
   
     
   // this.urlSafe = this.sanitizer.bypassSecurityTrustResourceUrl(this.url + '/' + jwt_token + '/' + this.userid
   //      + '/' + this.jtid + '/' + this.projectid + '/' + enviroment);
    }

}
