import {actionPromise} from "../reducers/PromiseReducer";
import {backURL, gql} from "./PathDB";
import {actionFullUserFindOne} from "./ActionUserFind";

export const actionUploadFile = file => {
    let fd = new FormData()
    fd.append('photo', file)
    return actionPromise('uploadFile', fetch(`${backURL}/upload`, {
        method: "POST",
        headers: localStorage.authToken ? {Authorization: 'Bearer ' + localStorage.authToken} : {},
        body: fd
    }).then(res => res.json()).catch(err => console.log(err)))
}

export const actionUploadFiles = array =>
    async dispatch => {
        for (const file of array) {
            await dispatch(actionUploadFile(file))
        }
    }

export const actionSetAvatar = file =>
     async (dispatch, getState) => {
         let result = await dispatch(actionUploadFile(file))
         if (result) {
             let value = await dispatch(actionPromise('setAvatar',
                 gql(`mutation setAvatar($myid: String, $imageid: ID){
                    UserUpsert(user:{
                                    _id: $myid, 
                                    avatar: {_id: $imageid}})
                    {
                        _id, avatar {
                            _id
                        }
                    }
                }`, {myid: getState().user._id, imageid: result._id})))
             if (value){
                 await dispatch(actionFullUserFindOne(getState().user._id))
             }
         }
     }

export const actionSetLogin = login =>
    async (dispatch, getState) => {
        let value = await dispatch(actionPromise('setNewLogin',
            gql(`mutation setNewLogin($myid: String, $login: String){
                    UserUpsert(user:{_id: $myid, login: $login}){
                        _id
                    }
                }`, {myid: getState().user._id, login: login})))
        if (value){
            await dispatch(actionFullUserFindOne(getState().user._id))
        }
    }

export const actionSetNick = nick =>
    async (dispatch, getState) => {
        let value = await dispatch(actionPromise('setNewNick',
            gql(`mutation setNewNick($myid: String, $nick: String){
                    UserUpsert(user:{_id: $myid, nick: $nick}){
                        _id
                    }
                }`, {myid: getState().user._id, nick: nick})))
        if (value){
            await dispatch(actionFullUserFindOne(getState().user._id))
        }
    }

export const actionSetPassword = password =>
    async (dispatch, getState) => {
        let value = await dispatch(actionPromise('setNewPassword',
            gql(`mutation setNewPassword($myid: String, $password: String){
                    UserUpsert(user:{_id: $myid, password: $password}){
                        _id
                    }
                }`, {myid: getState().user._id, password: password})))
        if (value){
            await dispatch(actionFullUserFindOne(getState().user._id))
        }
    }
