
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../../../services/auth.service';
import { FormBuilder, FormGroup, Validators, AbstractControl } from '@angular/forms';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { MatCheckboxModule, MatInputModule } from '@angular/material';
import _ from "lodash";
import { environment } from '../../../../../environments/environment.dev';

@Component({
  selector: 'app-awssandbox-conf',
  templateUrl: './awssandbox-conf.component.html',
  styleUrls: ['./awssandbox-conf.component.scss']
})

export class AWSSandboxConfComponent implements OnInit {

  img_path = environment.imageUrl+'/assets/img/Taflow/';
  prefs = [
    {name: 'Option 1'},
    {name: 'Option 2'}
   ];
  UpdateBtn = false;
   userForm: FormGroup;
    sandboxs = []
  dismiss = 'none'
  Skills: string;
  errMsg= '';
  sandboxes: any;
  tableData = {
    headers: ['Skill Master', 'Bundle ID', 'Workspace'],
    rows: [],
    dialogData: { title: 'AWS Sandbox', text: 'Would you like to delete this Sandbox from the list?'}
  };
  sandList: boolean = true;
  skilltrackResponse: Skilltrackdetails [];
  newsbFormdet: any;
  selectedSkillValues: any;
  ck: string;
  checked: boolean;
  input: MatInputModule;
  tileData: any;
  addNewSandboxbox: boolean= false;
  sbDetEdit: any;

  constructor(
    private fb: FormBuilder,
    private AuthService: AuthService,
    private toastr: ToastsManager) {

    this.getSandboxList()
    this.getAllSkills()
  }

  ngOnInit() {
    this.errMsg = ''
    this.UpdateBtn = false
    // this.navBarService.setCurrentPage(2);
    this.userForm = this.fb.group({
      directoryId: ['', Validators.required],
      bundleId: ['', Validators.required],
     // skillTrackMstrId : ['Select'],
      id : ['', Validators.required]
    });

   // this.input.underlineRef.nativeElement.className = null;
  
  }

  addSandbox(){
    this.sandList = false;
    this.addNewSandboxbox = true;    
    this.errMsg = ''
    this.UpdateBtn = false;
    //this.navBarService.setCurrentPage(2);
    this.userForm = this.fb.group({
      directoryId: ['', Validators.required],
      bundleId: ['', Validators.required],
      //skillTrackMstrId : ['Select'],
      id : ['', Validators.required]
    });
  }

  editSandbox(e){
    console.log(JSON.stringify(e));
    this.sandList = false;
    this.addNewSandboxbox = false; 
   this.sbDetEdit = e;
  }

  getSandboxList(){
  this.AuthService.getAllAwsDirectoryBundle().then(resp => {
      console.log(resp, 'getAllAwsDirectoryBundle');
      this.sandboxes = resp.pgAwsDirectoryBundleList;
      this.tableData.rows = this.sandboxes.map(function (item, index, array) {
        return [item.pgSkillTrackMstr.skillTrackName, item.bundleId, item.directoryId];
      });
    }).catch(err => {
      console.log(err);
    });
  }

  getAllSkills(){
    this.AuthService.getAllSkill().then(resp =>{
      console.log(resp,'getAllSkill')
      this.Skills = resp.pgSkillTrackDtlsList
      var customSkill = []
      var skills = _.uniqBy(resp.pgSkillTrackDtlsList,'pgSkillTrackMstr.skillTrackName')
      var masterSkills = _.uniqBy(_.flattenDeep(skills.map(i => i.pgSkillTrackMstr)))
      masterSkills.forEach(usrSkills => {
        var dtlsSkill = resp.pgSkillTrackDtlsList.filter(item => item.pgSkillTrackMstr.skillTrackName == usrSkills.skillTrackName)
        var skillTrackDtlsName = _.uniqBy(_.flattenDeep(dtlsSkill.filter(i => {return {id : i.skillTrackDtlsId, name : i.skillTrackDtlsName}})))
        customSkill.push({skillMaster:usrSkills,skillTrackDtls :dtlsSkill,skillTrackDtlsName:skillTrackDtlsName});
      });
      console.log(customSkill);
      this.skilltrackResponse = customSkill;
    }).catch(err =>{

      console.log(err)
    })

  }

  edit($event){
    this.newsbFormdet = $event;
    console.log(this.newsbFormdet);
    if(this.newsbFormdet != null || undefined){
      this.sandList = false;
    this.errMsg = ''
    this.UpdateBtn = true;
    }
    else{
      console.log(' Not yet cliked edit sandbox');
    }
  }

  // edit(index) {
  //   console.log('Editing item: ', this.sandboxes[index]);
  //   this.errMsg = ''
  //   this.UpdateBtn = true
  //    this.userForm= this.fb.group({
  //     directoryId: [this.sandboxes[index].directoryId, Validators.required],
  //     bundleId: [this.sandboxes[index].bundleId, Validators.required],
  //     skillTrackMstrId : [this.sandboxes[index].pgSkillTrackMstr.skillTrackMstrId],
  //     id : [this.sandboxes[index].id]
  //   });

  //   document.getElementById('openModalButton').click();
  // }

  deleteSandbox(id) {
   // var aws = id ;
    console.log(id)
    this.AuthService.getDeleteAwsDirecBundle(id).then(resp => {
      if (resp.status == 'error') {
        this.toastr.error(resp.message);
      } else {
        this.toastr.success(resp.successMessage);
        //this.tableData.rows.splice(index, 1);
        this.getSandboxList();
      }
    }).catch(err => {
      console.log(err)
    })
  }
  cancelForm(){
    this.sandList = true;
    this.newsbFormdet ={};
  }
  
  formCancelled(e){
    // console.log(e); 
    this.sandList = true;
    this.addNewSandboxbox = true;
    this.getSandboxList();
  }

 
}
interface Skilltrackdetails {
  pgSkillTrackDtlsList: {
  skillTrackDtlsId: number,
  pgSkillTrackMstr: {
      skillTrackMstrId: number,
      skillTrackName: string
  }
  skillTrackDtlsName: string
}
}