import { BaseStore } from "./base_store";
import {
  action,
  observable,
  computed
} from 'mobx';
import moment from 'moment';
import {omit} from 'lodash';
export class Messages extends BaseStore {

  @observable chatrooms = [];
  @observable activerooms = "";
  @observable selectedTab = "chat";
  @observable lineMessage = "";

  constructor(context) {
    super(context);
  }

  @action
  reset(){
    this.activerooms = "";
    this.chatrooms = [];
  }

  @action
  getRooms(){
    this.isLoading = true;
    return this.http.get("message/rooms")
      .then(res => {
        this.chatrooms = res.data;
        this.isLoading = false;
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      })
  }

  @action
  setRooms(val){
    this.activerooms = omit(val,['message']);
    this.isLoading = true;
    this.getRoomMessages(val.id);
    if(val.unread_message >0){
      this.readRoomMessages(val.id); 
    }
  }

  @action
  readRoomMessages(room_id){
    return this.http.put(`message/rooms/${room_id}/read`,{})
    .then(res => {
      
    }).catch(err => {
      throw err;
    })  
  }

  @action
  deliveredMessage(room_id,message_id){
    return this.http.put(`message/rooms/${room_id}/messages/${message_id}/delivered`,{})
    .then(res => {
      
    }).catch(err => {
      throw err;
    })  
  }

  @action
  readMessage(room_id,message_id){
    return this.http.put(`message/rooms/${room_id}/messages/${message_id}/read`,{})
    .then(res => {
      
    }).catch(err => {
      throw err;
    })  
  }

  @action
  getRoomMessages(id){
    return this.http.get(`message/rooms/${id}/messages`)
      .then(res => {
        let chatRoomIndex = this.chatrooms.findIndex(it=>it.id == id);
        this.chatrooms[chatRoomIndex].message = res;
        this.chatrooms[chatRoomIndex].unread_message = 0;
        this.chatrooms = this.chatrooms.map(it => it);
        this.isLoading = false;
        
      }).catch(err => {
        this.isLoading = false;
        throw err;
      })
      
  }

  @computed
  get message(){
    let data = [];
    if(this.activerooms !== ''){
      data = this.chatrooms.find(it=>it.id == this.activerooms.id).message;
    }
    return data;
  }

  @action
  changeTab(val){
    this.selectedTab= val;
  }

  @action
  sendMessage(){
    this.isLoading = true;
    if(!this.lineMessage){
      console.log('message empty');
      return 0;
    }
    return this.http.post(`message/rooms/${this.activerooms.id}/messages`,{
      message : this.lineMessage
    })
      .then(res => {
        // this.getRooms();
        this.lineMessage = "";
        this.isLoading = false;
      })
      .catch(err => {
        this.lineMessage = "";
        this.isLoading = false;
        throw err;
      })
  }

  @action
  writeMessage(val){
    this.lineMessage = val;
  }

  @action
  pushNewMessage(val){
      
    let chatRoomIndex = this.chatrooms.findIndex(it=>it.id == val.message_room_id);
    if(chatRoomIndex < 0){
      this.getRooms();
      return;
    }
    this.deliveredMessage(val.message_room_id,val.message_room_message_id);
    if(this.activerooms.id == val.message_room_id){
      this.readMessage(val.message_room_id,val.message_room_message_id);
    }else{
      this.chatrooms[chatRoomIndex].unread_message+=1;
    }
    val.yours_message = (val.yours_message == 'true');
    val.created_at = new Date().toISOString();
    val.user = JSON.parse(val.user);
    this.chatrooms[chatRoomIndex].message.push(val);

    let chatRoomData = this.chatrooms[chatRoomIndex];

    //move room to top
    this.chatrooms.splice(chatRoomIndex,1);
    this.chatrooms.splice(0,0,chatRoomData);
    //end

    this.chatrooms = this.chatrooms.map(it => it);
  }

}
