import { Component, OnInit, Input,Output, EventEmitter } from '@angular/core';
import {FormControl, Validators} from '@angular/forms';
import {SchedulePrimaryTestDataService} from './../service/schedule-primary-test-data.service';
import {Router} from '@angular/router';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import {AuthService} from '../../../services/auth.service'

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

  @Input() primarySchedule:any;
  @Output() schedule = new EventEmitter<string>();
  @Output() cancel = new EventEmitter<string>();


  assessmentList:any;
  assessmentId: string;
  assessmentControl = new FormControl('', [Validators.required]);
  respData:any;


  constructor(private schdulePrimaryTestService:SchedulePrimaryTestDataService,private router:Router,private toastr: ToastsManager, private AuthService:AuthService ) {

    console.log('this.schdulePrimaryTestService.getPrimaryTestData()',this.schdulePrimaryTestService.getPrimaryTestData());
    this.assessmentList = this.schdulePrimaryTestService.getPrimaryTestData();
  }

  ngOnInit() {

  }
   makeid() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 8; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}
  schedulePrimarytest($event){
    if($event === 'approve' && this.assessmentId){



        this.AuthService.scheduleCreate({assessmentId:this.assessmentId,name:this.makeid(),sourceApp:''}).then(res => {
          console.log(res)
          this.respData =  res
          console.log(this.respData.status)
          if(this.respData.status == 'success' || this.respData.status == 'SUCCESS'){

            console.log(this.respData)
            var results = JSON.parse(this.respData.data)
            if(results.error){
              this.toastr.error("The Assessment ID is not available at this moment.")
            }
            else {
              let reg = {
                "registrationDetails": {
                  "registrationDetails": [{
                    "uniqueId": this.makeid(),
                    "firstName": this.assessmentList[0].candidateInfo.fullName,
                    "email": this.assessmentList[0].candidateInfo.emailId
                  }]
                },
                "scheduleId": results.scheduleDetails.scheduleId
              }

              this.AuthService.registerUser(reg).then(res => {
                console.log(res);
                this.respData = res;
                console.log(this.respData.status);
                if (this.respData.status == 'success' || this.respData.status == 'SUCCESS') {

                  this.toastr.success('Mettl test scheduled Successfully');
                  this.router.navigate(['coach/landing']);


                }
                else{

                  setTimeout(()=>{

                  },6000);
                }
              }).catch(err=>{
                console.log(err);
                setTimeout(()=>{

                },6000);
              });

            }
          }
          else{
            setTimeout(()=>{

            },6000);
          }
        }).catch(err=>{
          console.log(err);
          setTimeout(()=>{

          },6000);
        });

    }
    else if($event === 'cancel'){
//todo -- get information on what should happen on click of cancel in schedule test screen
      this.router.navigate(['coach/landing']);

    }
  }
}
