const SocketIO = require('socket.io')
let io

const onSocketHandshakeAuthVerifier = (socket, next) => {

    console.log(`${new Date().toISOString()} ===========> MIDDLEWARE: Socket trying to connect with data ${JSON.stringify(socket.handshake.auth)} and origin ${socket.handshake.headers.origin}`)
    const codWeb = socket.handshake.auth.codWeb
    const extension = socket.handshake.auth.extension
    const origin = socket.handshake.headers.origin
    if (!origin) {
        console.log(`${new Date().toISOString()} ===========> MIDDLEWARE: Socket with data ${JSON.stringify(socket.handshake.auth)} disconnected because didn't send the origin`)
        return next(new Error(`Invalid handshake header information information origin must be specified`))
    }
    const isFromHitphoneWebClient = origin.includes(process.env.URL_HITPHONE_FRONTEND)
    //const isFromHitphoneWebClient = true
    if (!isFromHitphoneWebClient) {
        socket.data.isFromHitphoneWebClient = false
        return next()
    }

    if (!codWeb || !extension) {
        console.log(`${new Date().toISOString()} ===========> MIDDLEWARE: Socket with data ${JSON.stringify(socket.handshake.auth)} disconnected because didn't send extension or codWeb`)
        return next(new Error(`Invalid handshake auth information, required attributes codWeb, extension`))
    }

    socket.data.codWeb = codWeb
    socket.data.extension = extension
    socket.data.isFromHitphoneWebClient = true
    return next()
}

const onConnectionHitphoneWebClient = (socket) => {
    const { isFromHitphoneWebClient } = socket.data
    if (!isFromHitphoneWebClient) return

    console.log(`${new Date().toISOString()} ===========> SOCKET CONNECTION: Client connected from "Hitphone WEB Client"`)
    const { codWeb, extension } = socket.data
    socket.join(`${codWeb}@${extension}`)

    socket.on("disconnect", (data) => {
        console.log(`${new Date().toISOString()} ==========> SOCKET DISCONNECT: "Hitphone WEB Client" Client disconnected socket: ${data}`)
    })
}

const onConnectionCrmWizardClient = (socket) => {
    const { isFromHitphoneWebClient } = socket.data
    if (isFromHitphoneWebClient) return

    console.log(`${new Date().toISOString()} ===========> SOCKET CONNECTION: Client connected from "CRM Wizard client"`)
    const { codWeb, extension } = socket.data
    socket.join(`${codWeb}@${extension}`)

    socket.on("disconnect", (data) => {
        console.log(`${new Date().toISOString()} ==========> SOCKET DISCONNECT: "CRM Wizard client" Client disconnected, data: ${data}`)
    })
}

const initIO = (httpServer) => {
    const IS_DEV = process.env.IS_DEV ? Boolean(process.env.IS_DEV) : false

    io = SocketIO(httpServer, {
        cors: {
            origin: IS_DEV ? "*" : [process.env.URL_OAUTH_FRONTEND_SUCCESS_REDIRECT, 'https://integracao-crm.hitmanager.app.br', process.env.URL_HITPHONE_FRONTEND, "*"]
        },
        maxHttpBufferSize: 1e8
    })

    io.use(onSocketHandshakeAuthVerifier)
    /**
     * CRM Wizard Client
     */
    io.on("connection", onConnectionCrmWizardClient)
    /**
     * Hitphone Client Flow
     */
    io.on("connection", onConnectionHitphoneWebClient)

    /**
     * CRM template frontend test
    */
    io.on("connection", socket => {
        console.log('=======> CLIENT CONNECTED')

        socket.on("companySession", (companyId) => {
            console.log(`A client joined a companySession channel: ${companyId}`)
            socket.join(`company_${companyId}`)
        })

        socket.on("disconnect", (data) => {
            console.log(`Client disconnected socket: ${data}`)
        })
    })

    return io
}

const getIO = () => {
    if (!io) {
        console.log("Socket IO not initialized")
    }
    return io
}

module.exports = { initIO, getIO }
