import { Component, OnInit } from '@angular/core';
import { ModalController, NavParams, PopoverController } from '@ionic/angular';
import { ChatService } from 'src/app/services/chat.service';
import { ToastService } from 'src/app/services/toast.service';
import { ThemeService } from 'src/app/services/theme.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,
    private toastService: ToastService,
    public ThemeService: ThemeService
  ) {
    this.roomId = this.navParams.get('roomId');
  }

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

  close(action:any){
    if( window.innerWidth <= 701){
      this.popoverController.dismiss(action);
      this.modalController.dismiss(action);
    }
    else{
      this.popoverController.dismiss(action);
      this.modalController.dismiss(action);
    }
  }

   //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);
          this.close('leave');
        },
        (e)=>{
          if(e.error.errorType = "error-you-are-last-owner"){
            this.toastService.badRequest("Você é o último administrador do grupo. Por favor, defina o novo administrador antes de sair da grupo.");
            this.close('cancel');
          }
          else{
            this.toastService.badRequest("Não foi possível sair do grupo");
            this.close('cancel');
          }

        },
        ()=>{
          //loader.remove()
        });
      }
      else{
        this.chatService.leaveChannel(body).subscribe(res=>{
          console.log(res);
          this.close('leave');
        },
        (e)=>{
          if(e.error.errorType = "error-you-are-last-owner"){
            this.toastService.badRequest("Você é o último administrador do grupo. Por favor, defina o novo administrador antes de sair da grupo.");
            this.close('cancel');
          }
          else{
            this.toastService.badRequest("Não foi possível sair do grupo");
            this.close('cancel');
          }
        },
        ()=>{
          //loader.remove()
        });
      }
    });
  }

  //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(){
    if( window.innerWidth < 701){
      this.popoverController.dismiss('edit');
    }
    else{
      this.modalController.dismiss('edit');
    }
  }

}
