import { Component, OnInit } from '@angular/core';
import { ModalController, NavParams, PopoverController } from '@ionic/angular';
import { ChatService } from 'src/app/services/chat.service';

@Component({
  selector: 'app-chat-popover',
  templateUrl: './chat-popover.page.html',
  styleUrls: ['./chat-popover.page.scss'],
})
export class ChatPopoverPage implements OnInit {
  roomId:string;
  room: any;

  constructor(
    private popoverController: PopoverController,
    private modalController: ModalController,
    private navParams: NavParams,
    private chatService: ChatService,
  ) {
    this.roomId = this.navParams.get('roomId');
  }

  ngOnInit() {
    console.log(this.roomId);
  }

  close(){
    if( window.innerWidth <= 701){
      this.popoverController.dismiss('cancel');
      this.modalController.dismiss('cancel');
    }
    else{
      this.popoverController.dismiss('cancel');
      this.modalController.dismiss('cancel');
    }
  }

   //Top menu options
   //Close
   leaveGroup(){
     console.log('leave');

    let body = { "roomId":this.roomId, }

    this.chatService.getRoomInfo(this.roomId).subscribe(room=>{
      this.room = room['room'];

      if(this.room.t === 'p'){
        this.chatService.leaveGroup(body).subscribe(res=>{
          console.log(res);
        });
      }
      else{
        this.chatService.leaveChannel(body).subscribe(res=>{
          console.log(res);
        });
      }
    });
    this.popoverController.dismiss('leave');
    this.modalController.dismiss('leave');
  }

  //Delete
  deleteGroup(){
    let body = { "roomId":this.roomId, }
    this.chatService.getRoomInfo(this.roomId).subscribe(room=>{
      this.room = room['room'];

      if(this.room.t === 'p'){
        this.chatService.deleteGroup(body).subscribe(res=>{
          console.log(res);
        });
      }
      else{
        this.chatService.deleteChannel(body).subscribe(res=>{
          console.log(res);
        });
      }
    });
    this.popoverController.dismiss('delete');
    this.modalController.dismiss('delete');
  }

  async openChangeGroupName(){
    this.popoverController.dismiss('edit');
    this.modalController.dismiss('edit');
  }

}
