import { Component, OnInit, Inject } from '@angular/core';
import { MD_DIALOG_DATA, MdDialogRef } from '@angular/material';
import { ContactService } from '../../contact.service';

@Component({
  selector: 'app-shared-devices',
  templateUrl: './shared-devices.component.html',
  styleUrls: ['./shared-devices.component.scss']
})
export class SharedDevicesComponent implements OnInit {
  sharedWith = []
  device_id: any;

  constructor(public dialogRef: MdDialogRef<SharedDevicesComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService) { 
      this.device_id = this.data._id ; 
      this.getSharedDevice(this.device_id);
    }

  ngOnInit() {
   
  }

  DeleteDocumentsField(sharedObj){
    console.log('sharedObj',sharedObj);
    var device_id = sharedObj._id;

    var removeShareObject = {
      did : this.device_id,
      shared_id : sharedObj._id
    }
   console.log(removeShareObject);
   this.contactService.removeSharedDevice(removeShareObject).subscribe(res=>{
  
      this.getSharedDevice(this.device_id);
   },err=>{
    console.log(err);
   })
  }

  close(){
    this.dialogRef.close();
  }

  getSharedDevice(did){
    this.contactService.getSharedDevices(did).subscribe(res=>{
    
      this.sharedWith = res;
      console.log('this.sharedWith',this.sharedWith);
    },err=>{
      var errJson= JSON.parse(err['_body']); 
      console.log(errJson);
      if(errJson.message === "This Device is not Shared With Anyone"){
        this.dialogRef.close();

      }

    })

  }

}


