import { Component, OnInit, Inject, Injectable, Output, EventEmitter } from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material';
import { FormControl, Validators, FormGroup } from '@angular/forms';
import { EmployeeHttpService } from 'app/services/employee/employee-http.service';
import {MatSnackBar, MdSnackBarConfig} from '@angular/material';
import { SuccessComponentComponent } from '../../../../employee/shared/success-component/success-component.component';
import { LocalStorageService } from 'angular-2-local-storage';

@Injectable()
@Component({
  selector: 'app-create-environment',
  templateUrl: './create-environment.component.html',
  styleUrls: ['./create-environment.component.scss']
})
export class CreateEnvironmentComponent implements OnInit {
  projectInfo;
  createEnvironmentForm: FormGroup;
  error;
  projectDetails;
  prjUpdateResult;
  currentUser;
  @Output() getAllProjects = new EventEmitter();
  constructor(public dialogRef: MatDialogRef<CreateEnvironmentComponent>,
    @Inject(MAT_DIALOG_DATA) public prjInfo: any, private employeeService: EmployeeHttpService, public snackBar: MatSnackBar, protected localStorageService: LocalStorageService) {
      this.prjInfo = prjInfo.project;
      this.currentUser = this.localStorageService.get('userInfo');
    console.log('prjInfo', this.prjInfo);
  }

  ngOnInit() {
    console.log('opening dialog');
    this.createEnvironmentForm = new FormGroup({
      prjURL: new FormControl('', Validators.required),
      prjDes: new FormControl('', Validators.required),
    });
}

  createEnvironment(value) {
    this.error = false;
    if (value['prjURL'] === '' || value['prjURL'] === null || value['prjDes'] === '' || value['prjDes'] === null) {
      this.error = true;
      } else {
     this.projectDetails = {
      'projectId': this.prjInfo.projectId,
      'eclipseCheLink': value['prjURL'] ,
      'description': value['prjDes'],
      'updatedByName' : this.currentUser.fullName
    };
     this.employeeService.updateProjectDetails(this.projectDetails).subscribe(resp => {
          this.prjUpdateResult = resp;
           if (this.prjUpdateResult.error == null) {
            this.dialogRef.close();
            this.openSnackBar({msg : 'Successfully Created', type: 'success'});
            this.getAllProjects.emit();
            }
          // this.createProjectForm.reset();
      }, err => {
        this.dialogRef.close();
        this.openSnackBar({msg : 'Something Went Wrong!', type: 'error'});
      });
    }

  }
  onCancle() {
    this.dialogRef.close();
  }
  openSnackBar(result) {
    const extraClasses = (result.type === 'success') ? ['successSnackBar'] : ['errorSnackBar'] ;
    const icon = (result.type === 'success') ? 'done' : 'clear';
    this.snackBar.openFromComponent(SuccessComponentComponent, {
      duration: 1500,
      data: { message: result.msg, type: result.type, icon: icon},
      verticalPosition: 'top',
      extraClasses: extraClasses
    });
  }

}
