 export async function getLastMessages(chatId,msgLoaded){
   
   const response = await fetch(`http://localhost:8000/api/chat/get_some_messages/${chatId}?messages_loaded=${msgLoaded}&messages_to_get=15`,
   {
     method:'GET',
     credentials:'include'
   })

   if(response.ok){
     let msgMassive = await response.json();
     let localTime
     for(let i = 0; i < msgMassive.length; i++){
     localTime = new Date(msgMassive[i].created_at)
     msgMassive[i].created_at = localTime
     
     if(msgMassive[i].answer_id != null){

        let answer = await getMessageById(chatId,msgMassive[i].answer_id)
        msgMassive[i].answerMessage = answer.message
        //console.log(answer.message)
        
     }
     }
     msgMassive.reverse();
     return msgMassive
   }

    else if(response.status === 404){
    console.log(response)
   }
    
    else if(!response.ok)
    console.log(response.status)
    
   }
 

 export async function getMessageById(chatId,msgId){

  const response = await fetch(`http://localhost:8000/api/chat/message/${chatId}?message_id=${msgId}`,{
    method:"GET",
    credentials:'include'
  })

  if(response.ok){
    let data = await response.json(); 
    return data;
  }
  else{
    return { message: "Сообщение не найдено" };
  }

 }

 export async function MessagePicToUrl(messagePic){

   console.log(messagePic)
     const DataForm = new FormData();
     DataForm.append('file', messagePic)

     const respone = await fetch('http://localhost:8000/api/images/upload_image',
     {
       method:"POST",
       body:DataForm
     })

     if(!respone.ok)
     console.log("ошибка", respone.status)
  

     if(respone.ok){
       const data = await respone.json();
       console.log("картинка принята")
       return data.image_url;
       
     }
 }

 export async function getAllChats(){

  const response = await fetch(`http://localhost:8000/api/chat`,
  {
    method:"GET",
    credentials:'include'
  })
  if(response.ok){
    return await response.json();
  }
  else{
    console.log(response.status)
  }
 }

 export async function getPinnedMsg(ID){

  const response = await fetch(`http://localhost:8000/api/chat/pinned_messages/${ID}`,{
    method:"GET",
    credentials:'include'
  })

  if(response.ok){
    return await response.json();
  }
  else{
    console.log(response.status)
  }
 }

 export async function pinMessage(chatId,messageId){

  const response = await fetch(`http://localhost:8000/api/chat/pinn_message?chat_id=${chatId}&message_id=${messageId}`,{
  method:"POST",
  credentials:'include'
  })
  if(response.ok){
    return await response.json();
  }
  else
  console.log(response.status)

 }

 export async function unpinMessage(chatId, messageId){

  const response = await fetch(`http://localhost:8000/api/chat/unpinn_message?chat_id=${chatId}&message_id=${messageId}`,{
    method:"DELETE",
    credentials:'include'
    })
    if(response.ok){
      return await response.json();
    }
    else
      console.log(response.status)
  
   }
 
   export async function uploadImages(image) {

    const formData = new FormData();
      formData.append('file', image);

    const response = await fetch('http://localhost:8000/api/images/upload_image', {
      method: 'POST',
      credentials: 'include',
      body: formData,
    });
  
    if (response.ok) {
      let jopa = await response.json();
      return jopa.image_url
    } else {
      console.log(response);
    }
  }
  