import {history} from '../../history'
import actionPromise from "./action-promise";
import {gql} from '../../graphQl/getGQL'
import {actionAboutMe} from '../actions/action-aboutMe'
import {socket} from './../'

// import io from 'socket.io-client';

// const socket = io();


export const actionAuthLogin = (token) => ({ type: "AUTH_LOGIN", token });

export const actionFullLogin = (login, password) => async (dispatch) => {
    let token = await dispatch(
        actionPromise(
            "login",
            gql(
                `query log($login:String!, $password:String!){
                    login(login: $login, password: $password)
                  }`,
                { login: login, password: password }
            )
        )
    );
    if (token) {
        await dispatch(actionAuthLogin(token));
        await dispatch(actionAboutMe())
        history.push('/chat')
        
        
        // const socket = socketIO("ws://chat.ed.asmer.org.ua");
        // socket.on('msg', data => {
        //     console.log(JSON.stringify(data));
        //     alert(data);
        // });
        // socket.on('chat', data => {
        //     console.log(JSON.stringify(data));
        //     alert(data);
        // })
        // socket.on('chat_left', data => {
        //      console.log(JSON.stringify(data));
        //      alert(data);
        // })

        // debugger
    }
    if (!token) {
        history.push('/')
    }
};