import { I80F48, PerpMarket } from '@blockworks-foundation/mango-v4'
import SideBadge from '@components/shared/SideBadge'
import {
  Table,
  TableDateDisplay,
  Td,
  Th,
  TrBody,
  TrHead,
} from '@components/shared/TableElements'
import { NoSymbolIcon } from '@heroicons/react/20/solid'
import { PublicKey } from '@solana/web3.js'
import mangoStore from '@store/mangoStore'
import useMangoAccount from 'hooks/useMangoAccount'
import useSelectedMarket from 'hooks/useSelectedMarket'
import { useViewport } from 'hooks/useViewport'
import { useMemo } from 'react'
import { formatDecimal } from 'utils/numbers'
import { breakpoints } from 'utils/theme'
import TableMarketName from './TableMarketName'

const byTimestamp = (a: any, b: any) => {
  return (
    new Date(b.loadTimestamp || b.timestamp * 1000).getTime() -
    new Date(a.loadTimestamp || a.timestamp * 1000).getTime()
  )
}

const reverseSide = (side: string) => (side === 'buy' ? 'sell' : 'buy')

const parsedPerpEvent = (mangoAccountAddress: string, event: any) => {
  const maker = event.maker.toString() === mangoAccountAddress
  const orderId = maker ? event.makerOrderId : event.takerOrderId
  const value = event.quantity * event.price
  const feeRate = maker
    ? new I80F48(event.makerFee.val)
    : new I80F48(event.takerFee.val)
  const side = maker ? reverseSide(event.takerSide) : event.takerSide

  return {
    ...event,
    key: orderId?.toString(),
    liquidity: maker ? 'Maker' : 'Taker',
    size: event.size,
    price: event.price,
    value,
    feeCost: (feeRate.toNumber() * value).toFixed(4),
    side,
    marketName: event.marketName,
  }
}

const parsedSerumEvent = (event: any) => {
  let liquidity
  if (event.eventFlags) {
    liquidity = event?.eventFlags?.maker ? 'Maker' : 'Taker'
  } else {
    liquidity = event.maker ? 'Maker' : 'Taker'
  }
  return {
    ...event,
    liquidity,
    key: `${event.maker}-${event.price}`,
    value: event.price * event.size,
    side: event.side,
    marketName: event.marketName,
  }
}

const formatTradeHistory = (
  mangoAccountAddress: string,
  tradeHistory: any[]
) => {
  return tradeHistory
    .flat()
    .map((event) => {
      if (event.eventFlags || event.nativeQuantityPaid) {
        return parsedSerumEvent(event)
      } else if (event.maker) {
        return parsedPerpEvent(mangoAccountAddress, event)
      } else {
        return event
      }
    })
    .sort(byTimestamp)
}

const TradeHistory = () => {
  const group = mangoStore.getState().group
  const { selectedMarket } = useSelectedMarket()
  const { mangoAccount, mangoAccountAddress } = useMangoAccount()
  const fills = mangoStore((s) => s.selectedMarket.fills)
  const tradeHistory = mangoStore((s) => s.mangoAccount.tradeHistory)
  const { width } = useViewport()
  const showTableView = width ? width > breakpoints.md : false

  const openOrderOwner = useMemo(() => {
    if (!mangoAccount || !selectedMarket) return
    if (selectedMarket instanceof PerpMarket) {
      return mangoAccount.publicKey
    } else {
      try {
        return mangoAccount.getSerum3OoAccount(selectedMarket.marketIndex)
          .address
      } catch {
        console.warn(
          'Unable to find OO account for mkt index',
          selectedMarket.marketIndex
        )
      }
    }
  }, [mangoAccount, selectedMarket])

  const eventQueueFillsForAccount = useMemo(() => {
    if (!mangoAccountAddress || !selectedMarket) return []

    const mangoAccountFills = fills
      .filter((fill: any) => {
        if (fill.openOrders) {
          // handles serum event queue for spot trades
          return openOrderOwner ? fill.openOrders.equals(openOrderOwner) : false
        } else {
          // handles mango event queue for perp trades
          return (
            fill.taker.equals(openOrderOwner) ||
            fill.maker.equals(openOrderOwner)
          )
        }
      })
      .map((fill: any) => ({ ...fill, marketName: selectedMarket.name }))

    return formatTradeHistory(mangoAccountAddress, mangoAccountFills)
  }, [selectedMarket, mangoAccountAddress, openOrderOwner, fills])

  const combinedTradeHistory = useMemo(() => {
    let newFills = []
    if (eventQueueFillsForAccount?.length) {
      console.log('eventQueueFillsForAccount', eventQueueFillsForAccount)

      newFills = eventQueueFillsForAccount.filter((fill) => {
        return !tradeHistory.find((t) => {
          if (t.order_id) {
            return t.order_id === fill.orderId?.toString()
          }
          // else {
          //   return t.seq_num === fill.seqNum?.toString()
          // }
        })
      })
    }
    return [...newFills, ...tradeHistory]
  }, [eventQueueFillsForAccount, tradeHistory])

  console.log('trade history', tradeHistory)

  if (!selectedMarket || !group) return null

  return mangoAccount && combinedTradeHistory.length ? (
    showTableView ? (
      <div>
        <Table>
          <thead>
            <TrHead>
              <Th className="text-left">Market</Th>
              <Th className="text-right">Side</Th>
              <Th className="text-right">Size</Th>
              <Th className="text-right">Price</Th>
              <Th className="text-right">Value</Th>
              <Th className="text-right">Fee</Th>
              <Th className="text-right">Time</Th>
            </TrHead>
          </thead>
          <tbody>
            {combinedTradeHistory.map((trade: any) => {
              let market
              if ('market' in trade) {
                market = group.getSerum3MarketByExternalMarket(
                  new PublicKey(trade.market)
                )
              } else {
                market = selectedMarket
              }
              let makerTaker = trade.liquidity
              if ('maker' in trade) {
                makerTaker = trade.maker ? 'Maker' : 'Taker'
              }

              return (
                <TrBody
                  key={`${trade.signature || trade.marketIndex}${trade.size}`}
                  className="my-1 p-2"
                >
                  <Td className="">
                    <TableMarketName market={market} />
                  </Td>
                  <Td className="text-right">
                    <SideBadge side={trade.side} />
                  </Td>
                  <Td className="text-right font-mono">{trade.size}</Td>
                  <Td className="text-right font-mono">
                    {formatDecimal(trade.price)}
                  </Td>
                  <Td className="text-right font-mono">
                    {trade.price * trade.size}
                  </Td>
                  <Td className="text-right">
                    <span className="font-mono">
                      {trade.fee_cost || trade.feeCost}
                    </span>
                    <p className="font-body text-xs text-th-fgd-4">
                      {makerTaker}
                    </p>
                  </Td>

                  <Td className="whitespace-nowrap text-right">
                    {trade.block_datetime ? (
                      <TableDateDisplay
                        date={trade.block_datetime}
                        showSeconds
                      />
                    ) : (
                      'Recent'
                    )}
                  </Td>
                </TrBody>
              )
            })}
          </tbody>
        </Table>
      </div>
    ) : (
      <div>
        {eventQueueFillsForAccount.map((trade: any) => {
          return (
            <div
              className="flex items-center justify-between border-b border-th-bkg-3 p-4"
              key={`${trade.marketIndex}`}
            >
              <div>
                <TableMarketName market={selectedMarket} />
                <div className="mt-1 flex items-center space-x-1">
                  <SideBadge side={trade.side} />
                  <p className="text-th-fgd-4">
                    <span className="font-mono text-th-fgd-3">
                      {trade.size}
                    </span>
                    {' for '}
                    <span className="font-mono text-th-fgd-3">
                      {formatDecimal(trade.price)}
                    </span>
                  </p>
                </div>
              </div>
              <p className="font-mono">${trade.value.toFixed(2)}</p>
            </div>
          )
        })}
      </div>
    )
  ) : (
    <div className="flex flex-col items-center justify-center px-6 pb-8 pt-4">
      <NoSymbolIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
      <p>No trade history</p>
    </div>
  )
}

export default TradeHistory
