import { history } from "../App";
import { gql } from "../helpers/gql";
import { uploadFile } from "../helpers/uploadFile";
import { actionAboutMe } from "./actionAboutMe";
import { actionUpdateChat } from "./actionsForChats";
import { actionPromise } from "./actionsPromise";

const actionAddMedia = (typeId, mediaId, type) => 
actionPromise('newMedia', gql(`mutation setMedia($media: MediaInput) {
  MediaUpsert(media: $media) {
    _id
    
  }
}`, {media: {_id: mediaId, [type]: {_id: typeId}}}))


export const actionUploadFile = (file) => 
actionPromise('file', uploadFile(file))

export const actionUploadFiles = (files) => 
actionPromise('files', Promise.all(files.map(file => uploadFile(file))))          



export const actionSetUserAvatar =  (file) => 
  async (dispatch, getState) => {
    let userId = getState().auth.payload.sub.id;
      let media =  await dispatch(actionUploadFile(file));
      console.log(media)
      if (media._id){
         let dataId = await dispatch(actionAddMedia(userId, media._id, "userAvatar"))
         if (dataId){
             dispatch(actionAboutMe())
         }
      }
  }

export const actionSetChatAvatar = (file) => 
  async (dispatch, getState) => {
    const [,route, chatId] = history.location.pathname.split('/');
    const chat = getState().chats[chatId];
    
    const media = await dispatch(actionUploadFile(file));
    console.log(media)
    if(media._id){
      const dataId = await dispatch(actionAddMedia(chatId, media._id, "chatAvatars"))
      if (dataId){
        //it helps to everyone to get changed chat with avatar from socket (something to change for example title)
        dispatch(actionUpdateChat({_id: chatId, title: chat.title}));
        
      }
      return dataId
    }
  }


// const actionAddAvatar = (userId, avatarId) => 
// actionPromise('avatar', gql(`mutation setAvatar($userId: ID, $avatarId: ID){
//   UserUpsert(user:{_id: $userId, avatar: {_id: $avatarId}}){
//       _id, avatar{
//           _id
//       }
//   }
// }`,{userId: userId, avatarId: avatarId}))