import io from 'socket.io-client'
import PubSub from 'pubsub-js';

// const [isConnected, setIsConnected] = useState(socket.connected);
// const [fooEvents, setFooEvents] = useState([]);

class AccountAuth {
    constructor(){
        const URL = '121.42.8.157:19702'
        this._socket = io(URL, {
            transports: ["websocket", "polling"],
            // path: '',
            autoConnect: true,
        });

        // console.log("AccountAuth connected", this._socket.connected, this._socket);
        this._socket.on('connect', this.onConnect);
        this._socket.on('connect_error', this.onError);
        this._socket.on('error', this.onError);
        this._socket.on('connect_timeout', this.onTimeout);
        this._socket.on('disconnect', this.onDisconnect);
        this.authed = false;
    }
    
    onConnect = () => {
        setTimeout(() => {
            var sql = `SELECT 
                        map_id,
                        map_data, 
                        map_name, 
                        create_time, 
                        map_uuid, 
                        update_time
                    FROM 
                        medba.dat_map 
                    WHERE 
                        map_uuid = '${window.mapUUID}'`;
            var msg = {
                cmd: "query", // server端要进行判断的
                data: {
                    name: "name", // 这个无所谓怎么写，有就行
                    sql: sql,
                },
            };
            this._socket.emit("REPT", msg, (datas) => {
                // console.log(datas.data);
                const result = datas.data;
                window.editor_user = {
					maps: []
				};
                for (let i = 0; i < result.length; i++) {
                    const map = result[i];
                    // console.log(map);
                    window.editor_user.maps.push(map);
                }

                PubSub.publish('body.map.got');
            });  
        }, 1000);
    }
    
    onError(e) {
        console.log("error", e);
    }
    
    onTimeout(e) {
        console.log("timeout", e);
    }

    onDisconnect(e) {
        console.log("disconnected", e);
    }

    authenticated(){
        return this.authed;
    }
}
export default AccountAuth;