//Import the modules

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/toPromise';

import { environment } from '../../environments/environment.dev';

@Injectable()
export class WorkspaceService {

//Base Urls
  private baseURL= environment.apiUrl


  private createWorkspacesUrl = this.baseURL +"orchestrate/createWorkspaces";
  private describeWorkspacesUrl = this.baseURL +"orchestrate/describeWorkspaceDirectory";
  private describeWorkspaceUrl = this.baseURL +"orchestrate/describeWorkspaces";
  private startWorkspacesUrl = this.baseURL + "orchestrate/startWorkspaces";
  private saveLaunchDataUrl = this.baseURL + "orchestrate/saveLaunchData";
  private stopWorkspacesUrl = this.baseURL + "orchestrate/stopWorkspaces";
  private terminateWorkspacesUrl = this.baseURL +"orchestrate/terminateWorkspaces ";
  private rebuildWorkspacesUrl = this.baseURL +"orchestrate/rebuildWorkspaces";
  private WorkspaceFilesUrl = this.baseURL +"orchestrate/getWorkspaceFiles";
  private savecomputerNameUrl = this.baseURL +'orchestrate/updateComputerName/';
  private deleteWorkspaceUserUrl = this.baseURL + 'orchestrate/deleteWorkspaceUser';

  results: string[];
  data: Object;
  userData: any;
  signupData: any;
  loginData: any;
  creatWorkspaceData: any;
  startWorkspaceData:any;
  saveComputerNameInfo:any;
  stopWorkspaceData:any;
  terminateWorkspaceData:any;
  rebuildWorkspaceData:any;
  workspaceInfo:any;
deleteWorkspaceUserData:any;
  constructor(private http: Http) {}



  createWorkspace(user){

console.log(user);
    var output = 'success';

    return this.http.post(this.createWorkspacesUrl,user).toPromise().then(response => {
      this.creatWorkspaceData = response;
      console.log(response);
      console.log(this.creatWorkspaceData);
      if(this.creatWorkspaceData.status == 200){
        return this.creatWorkspaceData._body
      }
      else if(this.creatWorkspaceData.status == 400) {
        return {status:'error',message:'Invalid credentials...!'}
      }
      else {
        return {status:'error',message:'Something went wrong...!'}
      }

    })
  }

  describeWorkspace(user){

console.log(user)
    var output = 'success';

    return this.http.post(this.describeWorkspacesUrl,user).toPromise().then(response => {
      this.creatWorkspaceData = response;
      console.log(response);
      console.log(this.creatWorkspaceData);
      if(this.creatWorkspaceData.status == 200){
        return this.creatWorkspaceData._body
      }
      else if(this.creatWorkspaceData.status == 400) {
        return {status:'error',message:''}
      }
      else {
        return {status:'error',message:'Something went wrong...!'}
      }

    })
  }


  startWorkspace(Id){

    var output = 'success';

    return this.http.post(this.startWorkspacesUrl,Id).toPromise().then(response => {
      this.startWorkspaceData = response;
      console.log(response);
      console.log(this.startWorkspaceData);
      if(this.startWorkspaceData.status == 200){
        return this.startWorkspaceData._body
      }
      else if(this.startWorkspaceData.status == 400) {
        return {status:'error',message:'Invalid credentials...!'}
      }
      else {
        return {status:'error',message:'Something went wrong...!'}
      }

    })
  }


  saveLaunchData(Id){

    var output = 'success';

    return this.http.post(this.saveLaunchDataUrl,Id).toPromise().then(response => {
      this.startWorkspaceData = response;
      console.log(response);
      console.log(this.startWorkspaceData);
      if(this.startWorkspaceData.status == 200){
        return this.startWorkspaceData._body
      }
      else if(this.startWorkspaceData.status == 400) {
        return {status:'error',message:'Invalid credentials...!'}
      }
      else {
        return {status:'error',message:'Something went wrong...!'}
      }

    })
  }

  stopWorkspaces(Id){
    var output = 'success';

    return this.http.post(this.stopWorkspacesUrl,Id).toPromise().then(response => {
      this.stopWorkspaceData = response;
      console.log(response);
      console.log(this.stopWorkspaceData);
      if(this.stopWorkspaceData.status == 200){
        return this.stopWorkspaceData._body
      }
      else if(this.stopWorkspaceData.status == 400) {
        return {status:'error',message:'Invalid credentials...!'}
      }
      else {
        return {status:'error',message:'Something went wrong...!'}
      }

    })
  }

  terminateWorkspaces(Id){
    var output = 'success';

    return this.http.post(this.terminateWorkspacesUrl,Id).toPromise().then(response => {
      this.terminateWorkspaceData = response;
      console.log(response);
      console.log(this.terminateWorkspaceData);
      if(this.terminateWorkspaceData.status == 200){
        return this.terminateWorkspaceData.status
      }
      else if(this.terminateWorkspaceData.status == 400) {
        return {status:'error',message:'Invalid credentials...!'}
      }
      else {
        return {status:'error',message:'Something went wrong...!'}
      }

    })
  }

  deleteWorkspace(user){

    console.log(user);
    var output = 'success';

    return this.http.post(this.deleteWorkspaceUserUrl,user).toPromise().then(response => {
      this.deleteWorkspaceUserData = response;
      console.log(response);
      console.log(this.deleteWorkspaceUserData);
      if(this.deleteWorkspaceUserData.status == 200){
        return this.deleteWorkspaceUserData._body
      }
      else if(this.deleteWorkspaceUserData.status == 400) {
        return {status:'error',message:'Invalid credentials...!'}
      }
      else {
        return {status:'error',message:'Something went wrong...!'}
      }

    })
  }

  rebuildWorkspaces(Id){
    var output = 'success';

    return this.http.post(this.rebuildWorkspacesUrl,Id).toPromise().then(response => {
      this.rebuildWorkspaceData = response;
      console.log(response);
      console.log(this.rebuildWorkspaceData);
      if(this.rebuildWorkspaceData.status == 200){
        return this.rebuildWorkspaceData._body
      }
      else if(this.rebuildWorkspaceData.status == 400) {
        return {status:'error',message:'Invalid credentials...!'}
      }
      else {
        return {status:'error',message:'Something went wrong...!'}
      }

    })
  }

  setWorkspaceId(user, workspaceId){

    this.workspaceInfo = {
      "user": user,
      "workspaceId": workspaceId
    };

  }

  getWorkspaceId(){
    return this.workspaceInfo;
  }
  /*aws workspace creation*/
//Handling the Error function here...
  private handleError(error: any)
  {
    return Promise.reject({

      status:'error',message:'Something went wrong...!'

    });
  }

  describeWorkspaceById(user){

    var output = 'success';

    return this.http.post(this.describeWorkspaceUrl,user).toPromise().then(response => {
      this.creatWorkspaceData = response;
      console.log(response);
      console.log(this.creatWorkspaceData);
      if(this.creatWorkspaceData.status == 200){
        return this.creatWorkspaceData._body
      }
      else if(this.creatWorkspaceData.status == 400) {
        return {status:'error',message:''}
      }
      else {
        return {status:'error',message:'Something went wrong describeWorkspaceById-workspace.service.ts...!'}
      }

    })
  }

  getWorkspaceFiles(user){
    console.log(user)
    var output = 'success'
    return this.http.post(this.WorkspaceFilesUrl,user).toPromise().then(response => {

      this.saveComputerNameInfo = response;
      console.log(response);
      console.log(this.saveComputerNameInfo,'saveComputerNameInfo');
      if(this.saveComputerNameInfo.status == 200){
        return this.saveComputerNameInfo._body
      }
      else if(this.saveComputerNameInfo.status == 400) {
        return {status:'error',message:'Invalid credentials...!'}
      }
      else {
        return {status:'error',message:'Something went wrong...!'}
      }


    }).catch(err =>{
      var errData;
      console.log(err.status)
      console.log(err._body)

    })
  }

  saveComputerName(adid, compname) {

    var output = 'success';
    //if (adid && compname) {
      var URL = this.savecomputerNameUrl+ adid+'/'+compname;

      console.log(this.savecomputerNameUrl + adid + '/' + compname, "url");

      return this.http.get(URL).toPromise().then(response => {

        this.saveComputerNameInfo = response
        return  JSON.parse(this.saveComputerNameInfo._body)

      })
    }

 // }
}
