import {io} from "socket.io-client";
import {useStore} from "@/store/store.js";
import {flushOrders} from "@/blockchain/wallet.js";
import {parseOrderStatus} from "@/blockchain/orderlib.js";
import { DataFeed } from "./charts/datafeed";

export const socket = io(import.meta.env.VITE_WS_URL || undefined, {transports: ["websocket"]})

socket.on('connect', () => {
    console.log(new Date(), 'ws connected')
})

socket.on('disconnect', () => {
    console.log(new Date(), 'ws disconnected')
})

socket.on('p', async (chainId, pool, price) => {
    // console.log('pool price from message', chainId, pool, price)
    const s = useStore()
    if( s.chainId !== chainId )
        return
    s.poolPrices[[chainId,pool]] = price
})

socket.on('ohlc', async (chainId, poolPeriod, ohlcs) => {
    // console.log('pool bars', poolPeriod, ohlcs)
    if (ohlcs && ohlcs.length) {
        const split = poolPeriod.indexOf('|')
        const pool = poolPeriod.slice(0,split)
        useStore().poolPrices[[chainId, pool]] = ohlcs[ohlcs.length - 1][4] // closing price
    }
    DataFeed.poolCallback(chainId, poolPeriod, ohlcs)
})

socket.on('vb', async (chainId, vault, balances) => {
    const s = useStore()
    if( s.chainId !== chainId )
        return
    console.log('vb', vault, balances)
    s.vaultBalances[vault] = JSON.parse(balances)
    console.log('vault balances', vault, s.vaultBalances[vault])
})

socket.on('vaults', (chainId, owner, vaults)=>{
    const s = useStore()
    console.log('vaults', chainId, owner, vaults)
    if( s.chainId !== chainId || s.account !== owner )
        return
    if( vaults.length > s.vaults.length ) {
        s.vaults = vaults
        if( vaults.length ) {
            const vault = vaults[0]
            flushOrders(vault)
        }
    }
})


function handleOrderStatus(chainId, vault, orderIndex, status) {
    const s = useStore()
    if( s.chainId !== chainId )
        return
    // message 'o' is a single order status
    const parsed = parseOrderStatus(chainId, status);
    console.log('o', chainId, vault, orderIndex, status, parsed)
    if( !(vault in s.orders) )
        s.orders[vault] = {}
    s.orders[vault][orderIndex] = parsed
}

socket.on('os', (chainId, vault,  orders) => {
    // message 'os' has multiple order statuses
    console.log('os', orders)
    for( const [orderIndex, status] of orders )
        handleOrderStatus(chainId, vault, orderIndex, status)
})

socket.on( 'o', handleOrderStatus)

socket.on( 'of', (chainId, vault, orderIndex, filled)=>{
    const s = useStore()
    if( s.chainId !== chainId )
        return
    console.log('of', chainId, vault, orderIndex, filled)
    if( !(vault in s.orders) ) {
        console.log('warning: got fill on an order in an unknown vault')
        return
    }
    if( !(orderIndex in s.orders[vault]) ) {
        console.log(`warning: orderIndex ${orderIndex} missing from vault ${vault}`)
        return
    }

    const status = s.orders[vault][orderIndex]
    console.log('apply fills', status, filled)
    status.filledIn = BigInt(filled[0][0])
    status.filledOut = BigInt(filled[0][1])
    for( const i in filled[1] ) {
        const [filledIn, filledOut] = filled[1][i]
        status.trancheFilledIn[i] = BigInt(filledIn)
        status.trancheFilledOut[i] = BigInt(filledOut)
    }
    // s.orders[vault][orderIndex] = status
    console.log('applied fills', status)
})
