import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NavBarService } from '../admin.service';
import { AuthService } from '../../services/auth.service';
import { FormBuilder, FormGroup, Validators, AbstractControl } from '@angular/forms';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import _ from "lodash";
import { environment } from '../../../environments/environment.dev';

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

  img_path = environment.imageUrl+'/assets/img/admin/';
  prefs = [
    {name: 'Option 1'},
    {name: 'Option 2'}
  ];
  userForm: FormGroup;
  roles = []
  successResp = true
  dismiss = 'none';
  errMsg = '';
  masterSkills: any= []
  tableData = {
    headers: ['Skillmaster', 'Skill Category', 'Skill Set'],
    rows: [],
    dialogData: { title: 'Skill Sets', text: 'Would you like to delete this skill set from the list?'}
  };

  skills = {
    "pgSkillTrackMstr": null,
    "pgSkillTrackDtlsList": [{
      "skillTrackDtlsId": 1,
      "pgSkillTrackMstr": {
        "skillTrackMstrId": 1,
        "skillTrackName": "JavaScript",
        "description": "JavaScript",
        "status": "ACTIVE",
        "createdBy": "system",
        "createdDt": 1509104683000,
        "updatedBy": "system",
        "updatedDt": 1509104683000
      },
      "pgSkillCategory": {
        "skillCategoryId": 1,
        "skillCategoryName": "Front End",
        "description": "Front End",
        "status": "ACTIVE",
        "createdBy": "USER",
        "createdDt": 1509104683000,
        "updatedBy": "USER",
        "updatedDt": 1509104683000
      },
      "skillTrackDtlsName": "Angular JS",
      "description": "Angular JS",
      "createdBy": "system",
      "createdDt": 1509104684000,
      "updatedBy": "system",
      "updatedDt": 1509104684000
    },
      {
        "skillTrackDtlsId": 2,
        "pgSkillTrackMstr": {
          "skillTrackMstrId": 1,
          "skillTrackName": "JavaScript",
          "description": "JavaScript",
          "status": "ACTIVE",
          "createdBy": "system",
          "createdDt": 1509104683000,
          "updatedBy": "system",
          "updatedDt": 1509104683000
        },
        "pgSkillCategory": {
          "skillCategoryId": 1,
          "skillCategoryName": "Front End",
          "description": "Front End",
          "status": "ACTIVE",
          "createdBy": "USER",
          "createdDt": 1509104683000,
          "updatedBy": "USER",
          "updatedDt": 1509104683000
        },
        "skillTrackDtlsName": "React JS",
        "description": "React JS",
        "createdBy": "system",
        "createdDt": 1509104684000,
        "updatedBy": "system",
        "updatedDt": 1509104684000
      },
      {
        "skillTrackDtlsId": 3,
        "pgSkillTrackMstr": {
          "skillTrackMstrId": 1,
          "skillTrackName": "JavaScript",
          "description": "JavaScript",
          "status": "ACTIVE",
          "createdBy": "system",
          "createdDt": 1509104683000,
          "updatedBy": "system",
          "updatedDt": 1509104683000
        },
        "pgSkillCategory": {
          "skillCategoryId": 1,
          "skillCategoryName": "Front End",
          "description": "Front End",
          "status": "ACTIVE",
          "createdBy": "USER",
          "createdDt": 1509104683000,
          "updatedBy": "USER",
          "updatedDt": 1509104683000
        },
        "skillTrackDtlsName": "ExtJS",
        "description": "ExtJS",
        "createdBy": "system",
        "createdDt": 1509104684000,
        "updatedBy": "system",
        "updatedDt": 1509104684000
      },
      {
        "skillTrackDtlsId": 4,
        "pgSkillTrackMstr": {
          "skillTrackMstrId": 1,
          "skillTrackName": "JavaScript",
          "description": "JavaScript",
          "status": "ACTIVE",
          "createdBy": "system",
          "createdDt": 1509104683000,
          "updatedBy": "system",
          "updatedDt": 1509104683000
        },
        "pgSkillCategory": {
          "skillCategoryId": 2,
          "skillCategoryName": "Back End",
          "description": "Back End",
          "status": "ACTIVE",
          "createdBy": "USER",
          "createdDt": 1509104683000,
          "updatedBy": "USER",
          "updatedDt": 1509104683000
        },
        "skillTrackDtlsName": "Node JS",
        "description": "Node JS",
        "createdBy": "system",
        "createdDt": 1509104684000,
        "updatedBy": "system",
        "updatedDt": 1509104684000
      },
      {
        "skillTrackDtlsId": 5,
        "pgSkillTrackMstr": {
          "skillTrackMstrId": 2,
          "skillTrackName": "Java",
          "description": "Java",
          "status": "ACTIVE",
          "createdBy": "system",
          "createdDt": 1509104683000,
          "updatedBy": "system",
          "updatedDt": 1509104683000
        },
        "pgSkillCategory": {
          "skillCategoryId": 1,
          "skillCategoryName": "Front End",
          "description": "Front End",
          "status": "ACTIVE",
          "createdBy": "USER",
          "createdDt": 1509104683000,
          "updatedBy": "USER",
          "updatedDt": 1509104683000
        },
        "skillTrackDtlsName": "JSP",
        "description": "JSP",
        "createdBy": "system",
        "createdDt": 1509104684000,
        "updatedBy": "system",
        "updatedDt": 1509104684000
      },
      {
        "skillTrackDtlsId": 6,
        "pgSkillTrackMstr": {
          "skillTrackMstrId": 2,
          "skillTrackName": "Java",
          "description": "Java",
          "status": "ACTIVE",
          "createdBy": "system",
          "createdDt": 1509104683000,
          "updatedBy": "system",
          "updatedDt": 1509104683000
        },
        "pgSkillCategory": {
          "skillCategoryId": 2,
          "skillCategoryName": "Back End",
          "description": "Back End",
          "status": "ACTIVE",
          "createdBy": "USER",
          "createdDt": 1509104683000,
          "updatedBy": "USER",
          "updatedDt": 1509104683000
        },
        "skillTrackDtlsName": "Core Java",
        "description": "Core Java",
        "createdBy": "system",
        "createdDt": 1509104684000,
        "updatedBy": "system",
        "updatedDt": 1509104684000
      }
    ],
    "skillTrackMstrId": null,
    "skillCategoryId": null,
    "skillTrackDtlsName": null,
    "description": null,
    "error": null,
    "pgSkillTrackDtls": null
  }

  constructor( private router: Router,
               private fb: FormBuilder,
               private navBarService: NavBarService,
               private AuthService: AuthService,
               private toastr: ToastsManager) { }

  ngOnInit() {
    this.navBarService.setCurrentPage(10);
    this.userForm = this.fb.group({
      skillMaster: ['', Validators.required],
      skillCategory: ['', Validators.required],
      skillDetails: ['', Validators.required],
    });
    this.getAllSkillTrackDtls();
  }





  onSubmit(value: string): void {
    this.errMsg = '';
    console.log('you submitted value Role: ', value);

    if (value['skillMaster'] == '' || value['skillMaster'] == 'undefined' || value['skillMaster']== null ){
      this.successResp = false;
      this.errMsg = "Please enter valid skillMaster";

    } else {
      // this.AuthService.getAddRole(value).then(resp => {
      //   if (resp.status == 'error') {
      //     console.log(resp, 'getAddRole response error')
      //     this.successResp = false;
      //     this.errMsg = resp.message;
      //   }
      //   else {
      //     if (resp.error == null) {
      //       this.toastr.success('Role added successfully');
      //       this.getAllRole()
      //       this.navBarService.setCurrentPage(6);
      //       this.userForm = this.fb.group({
      //         roleName: ['', Validators.required],
      //       });
      //       this.dismiss = 'modal';
      //     } else {
      //       console.log(resp, 'Error response');
      //     }
      //   }
      //
      // }).catch(err => {
      //
      //   console.log(err)
      // })
    }
  }

  getAllRole() {


    console.log(this.skills.pgSkillTrackDtlsList.length)
    console.log(this.skills)
    this.masterSkills = _.uniqBy(this.skills.pgSkillTrackDtlsList,'pgSkillTrackMstr.skillTrackMstrId');
    console.log('table, ', this.masterSkills);
    const skillsDtlsList = this.skills.pgSkillTrackDtlsList;
    this.tableData.rows = this.masterSkills.map(function (item, index, array) {
      console.log('iteration ', item.pgSkillTrackMstr.skillTrackMstrId);
      const skillsList = [];
      skillsDtlsList.forEach(usrSkills => {
        if (item.pgSkillTrackMstr.skillTrackMstrId == usrSkills.pgSkillTrackMstr.skillTrackMstrId) {
          skillsList.push(usrSkills);
        }
      });
      console.log('skillslist ', skillsList)
      const joinedSkills = skillsList.reduce( function(prevElem, elem) {
        console.log('joined ', elem);
        return prevElem + '\t' + elem.description;
      }, '');

      console.log(joinedSkills)
      return [item.pgSkillTrackMstr.description,item.pgSkillCategory.description, joinedSkills];
    });
    console.log(this.masterSkills.length)
  }


  skillMstrs(skillId) {
    console.log('skillsmstrs');
    let masterSkillId = []
    this.skills.pgSkillTrackDtlsList.forEach(usrSkills => {
      if (skillId == usrSkills.pgSkillTrackMstr.skillTrackMstrId) {
        masterSkillId.push(usrSkills);
      }
    });
    return masterSkillId;
  }



  getAllSkillTrackDtls(){


    this.AuthService.getAllSkill().then(resp =>{

      console.log(resp,'getAllSkill')

      this.skills = resp

      this.getAllRole();
      //  this.roles = resp.roleList
    }).catch(err =>{

      console.log(err)
    })

  }


  openModel() {
    this.errMsg = '';
    this.userForm = this.fb.group({
      skillMaster: ['', Validators.required],
      skillCategory: ['', Validators.required],
      skillDetails: ['', Validators.required],
    });
  }

  edit(index) {
    console.log('Editing item: ', this.masterSkills[index]);
    this.router.navigate(['/admin/skill/' + this.masterSkills[index].pgSkillTrackMstr.skillTrackMstrId]);
  }

  delete(index) {
    console.log('Deleting item: ', this.masterSkills[index]);
    this.tableData.rows.splice(index, 1);
  }

}
