import { Component, OnInit, Output, EventEmitter, Input } 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";

@Component({
  selector: 'app-sandboxform',
  templateUrl: './sandboxform.component.html',
  styleUrls: ['./sandboxform.component.scss']
})
export class SandboxformComponent implements OnInit {

  errMsg= '';
  //skilltrackResponse: Skilltrackdetails [];
  Skills: string;
  skilltrackResponse: Skilltrackdetails [];
  newsbFormdet: any;
  selectedSkillValues: any;
  ck: string;
  checked: boolean;
  input: MatInputModule;
  tileData: any;
  UpdateBtn: boolean;
  editDisabled: boolean;

  @Output() closeFormEvent = new EventEmitter<string>();

  @Input() boxEditDet: any;

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

    // this.AuthService.getSandboxList()
     this.getAllSkills()
     }

  ngOnInit() {
    this.newsbFormdet ={};
    if(this.boxEditDet){
      console.log(JSON.stringify(this.boxEditDet));
      this.newsbFormdet.id = this.boxEditDet.id;
      this.newsbFormdet.skillTrackMstrId = this.boxEditDet.pgSkillTrackMstr.skillTrackMstrId;
       this.selectedSkillValues = this.newsbFormdet.skillTrackMstrId;
      this.newsbFormdet.skillTrackName = this.boxEditDet.pgSkillTrackMstr.skillTrackName;
      this.newsbFormdet.directoryId = this.boxEditDet.directoryId;
      this.newsbFormdet.bundleId = this.boxEditDet.bundleId 
      this.newsbFormdet.description =this.boxEditDet.description;
      this.UpdateBtn = true;
      this.editDisabled = true;
    }
  }

  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)
    })
  }

  onSkillMaster(e, data){
    // console.log(e);
    // console.log("data -- "+JSON.stringify(data));
    this.selectedSkillValues = data;
    // console.log(this.selectedSkillValues);
  }

  onSubmit(newsbFormdet) {
    // console.log('you submitted value: ' +JSON.stringify(newsbFormdet));

    this.errMsg = ''
    if (newsbFormdet.skillTrackMstrId == undefined || newsbFormdet.skillTrackMstrId == null){
      this.errMsg = "Please Select skill set";
    }else if (newsbFormdet.directoryId == '' || newsbFormdet.directoryId == 'undefined' || newsbFormdet.directoryId == null ){
      this.errMsg = "Please enter valid directoryId";
    }else if (newsbFormdet.bundleId == '' || newsbFormdet.bundleId == 'undefined' || newsbFormdet.bundleId == null ){
      this.errMsg = "Please enter valid bundleId";
    
  }else if (newsbFormdet.description == '' || newsbFormdet.description == 'undefined' || newsbFormdet.description == null ){
    this.errMsg = "Please enter description";
  }else {

      //delete value['id']

      this.AuthService.getaddAWSDirecBundle(newsbFormdet).then(resp => {

        // console.log(resp, 'response')
        if (resp.status == 'error') {
          this.errMsg = resp.message
        }
        else {
          console.log(resp.error, 'responseELSeeeeeeeeee')
          this.toastr.success('AWS Sandbox added successfully');
          this.newsbFormdet = {};
          this.closeFormEvent.emit('true');
        }
      }).catch(err => {
        console.log(err, "Error")
      })
    }
  }

  onEditSubmit(newsbFormdet) {
    // console.log('you submitted value: ' +JSON.stringify(newsbFormdet));
    this.errMsg = ''
    if (newsbFormdet.directoryId == '' || newsbFormdet.directoryId == 'undefined' || newsbFormdet.directoryId == null ){
      this.errMsg = "Please enter valid directoryId";
    }else if (newsbFormdet.bundleId == '' || newsbFormdet.bundleId == 'undefined' || newsbFormdet.bundleId == null ){
      this.errMsg = "Please enter valid bundleId";
    
  }else if (newsbFormdet.description == '' || newsbFormdet.description == 'undefined' || newsbFormdet.description == null ){
    this.errMsg = "Please enter description";
  }else {

      this.AuthService.getUpdateAWSDirecBundle(newsbFormdet).then(resp => {
        // console.log(resp, 'response')
        if (resp.status == 'error') {
          this.errMsg = resp.message
        }
        else {
          console.log(resp.error, 'responseELSeeeeeeeeee')

          if (resp.error == null) {
            // this.userForm = this.fb.group({
            //   directoryId: ['', Validators.required],
            //   bundleId: ['', Validators.required],
            //   skillTrackMstrId: ['Select'],
            //   id: ['', Validators.required]
            // });
            this.UpdateBtn = false;
            this.toastr.success('AWS Sandbox updated successfully');
            this.newsbFormdet = {};
            this.closeFormEvent.emit('true');
          }
          else {
            console.log("Else of response")
          }
        }
      }).catch(err => {
        console.log(err, "Error")
      })
    }
  }
  cancelForm(){
    this.newsbFormdet = {};
    this.boxEditDet = {};
    this.closeFormEvent.emit('true');
    // console.log("cancel clicked");
  }
}

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