/**
 * LP 边界测试组件
 * 用于测试各种边界条件
 */

import { Button } from 'antd'
import { parseUnits } from 'viem'
import {
  GAS_CONFIG,
  YT_REWARD_ROUTER_ABI,
} from '@/config/contracts'
import type { TransactionType } from '@/context/TransactionContext'

interface LPBoundaryTestProps {
  address: `0x${string}` | undefined
  ytLPBalance?: bigint
  isProcessing: boolean
  writeContract: (config: any) => void
  recordTx: (type: TransactionType, amount?: string, token?: string) => void
  show: boolean
  onToggle: () => void
  rewardRouter: `0x${string}` | undefined
  vaultAddresses: string[]
  ytDecimals: number
  ytLpDecimals: number
}

export function LPBoundaryTest({
  address,
  ytLPBalance,
  isProcessing,
  writeContract,
  recordTx,
  show,
  onToggle,
  rewardRouter,
  vaultAddresses,
  ytDecimals,
  ytLpDecimals,
}: LPBoundaryTestProps) {

  const vault0 = vaultAddresses[0] as `0x${string}` | undefined
  const vault1 = vaultAddresses[1] as `0x${string}` | undefined
  const canTest = !!address && !!rewardRouter && !!vault0

  const runBoundaryTest = (testType: string) => {
    if (!canTest) return
    recordTx('test', undefined, 'LP')

    switch (testType) {
      case 'add_zero':
        writeContract({
          address: rewardRouter,
          abi: YT_REWARD_ROUTER_ABI,
          functionName: 'addLiquidity',
          args: [vault0, BigInt(0), BigInt(0), BigInt(0)],
          gas: GAS_CONFIG.COMPLEX,
        })
        break
      case 'add_exceed_balance':
        writeContract({
          address: rewardRouter,
          abi: YT_REWARD_ROUTER_ABI,
          functionName: 'addLiquidity',
          args: [vault0, parseUnits('999999999', ytDecimals), BigInt(0), BigInt(0)],
          gas: GAS_CONFIG.COMPLEX,
        })
        break
      case 'remove_zero':
        writeContract({
          address: rewardRouter,
          abi: YT_REWARD_ROUTER_ABI,
          functionName: 'removeLiquidity',
          args: [vault0, BigInt(0), BigInt(0), address],
          gas: GAS_CONFIG.COMPLEX,
        })
        break
      case 'remove_exceed_balance': {
        const exceedAmount = ytLPBalance
          ? ytLPBalance + parseUnits('999999', ytLpDecimals)
          : parseUnits('999999999', ytLpDecimals)
        writeContract({
          address: rewardRouter,
          abi: YT_REWARD_ROUTER_ABI,
          functionName: 'removeLiquidity',
          args: [vault0, exceedAmount, BigInt(0), address],
          gas: GAS_CONFIG.COMPLEX,
        })
        break
      }
      case 'remove_high_minout': {
        const testAmount = ytLPBalance && ytLPBalance > BigInt(0)
          ? (ytLPBalance > parseUnits('0.1', ytLpDecimals) ? parseUnits('0.1', ytLpDecimals) : ytLPBalance)
          : parseUnits('0.001', ytLpDecimals)
        writeContract({
          address: rewardRouter,
          abi: YT_REWARD_ROUTER_ABI,
          functionName: 'removeLiquidity',
          args: [vault0, testAmount, parseUnits('999999999', ytDecimals), address],
          gas: GAS_CONFIG.COMPLEX,
        })
        break
      }
      case 'swap_zero':
        writeContract({
          address: rewardRouter,
          abi: YT_REWARD_ROUTER_ABI,
          functionName: 'swapYT',
          args: [vault0, vault1 ?? vault0, BigInt(0), BigInt(0), address],
          gas: GAS_CONFIG.COMPLEX,
        })
        break
      case 'swap_same_token':
        writeContract({
          address: rewardRouter,
          abi: YT_REWARD_ROUTER_ABI,
          functionName: 'swapYT',
          args: [vault0, vault0, parseUnits('1', ytDecimals), BigInt(0), address],
          gas: GAS_CONFIG.COMPLEX,
        })
        break
      case 'swap_exceed_balance':
        writeContract({
          address: rewardRouter,
          abi: YT_REWARD_ROUTER_ABI,
          functionName: 'swapYT',
          args: [vault0, vault1 ?? vault0, parseUnits('999999999', ytDecimals), BigInt(0), address],
          gas: GAS_CONFIG.COMPLEX,
        })
        break
    }
  }

  return (
    <div style={{ marginTop: '12px', padding: '8px 12px', background: '#f5f5f5', borderRadius: '8px', border: '1px solid #e0e0e0' }}>
      <div
        style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', cursor: 'pointer' }}
        onClick={onToggle}
      >
        <h4 style={{ margin: 0, color: '#666', fontSize: '13px' }}>{'边界测试'}</h4>
        <span style={{ color: '#999', fontSize: '16px' }}>{show ? '▼' : '▶'}</span>
      </div>

      {show && (
        <div style={{ marginTop: '12px' }}>
          <p style={{ fontSize: '11px', color: '#666', marginBottom: '8px' }}>
            点击按钮测试各种边界条件，预期这些测试都会失败（合约应正确拒绝）
            {!canTest && <span style={{ color: '#ff4d4f' }}> — 需要连接钱包且池子有代币</span>}
          </p>

          <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(180px, 1fr))', gap: '8px' }}>
            <div style={{ padding: '8px', background: '#fff', borderRadius: '4px', border: '1px solid #e0e0e0' }}>
              <div style={{ fontSize: '11px', fontWeight: 'bold', color: '#333', marginBottom: '6px' }}>添加流动性</div>
              <Button size="small" onClick={() => runBoundaryTest('add_zero')} disabled={isProcessing || !canTest} style={{ fontSize: 12, marginRight: 4, marginBottom: 4 }}>金额=0</Button>
              <Button size="small" onClick={() => runBoundaryTest('add_exceed_balance')} disabled={isProcessing || !canTest} style={{ fontSize: 12 }}>超额</Button>
            </div>

            <div style={{ padding: '8px', background: '#fff', borderRadius: '4px', border: '1px solid #e0e0e0' }}>
              <div style={{ fontSize: '11px', fontWeight: 'bold', color: '#333', marginBottom: '6px' }}>移除流动性</div>
              <Button size="small" onClick={() => runBoundaryTest('remove_zero')} disabled={isProcessing || !canTest} style={{ fontSize: 12, marginRight: 4, marginBottom: 4 }}>金额=0</Button>
              <Button size="small" onClick={() => runBoundaryTest('remove_exceed_balance')} disabled={isProcessing || !canTest} style={{ fontSize: 12, marginRight: 4, marginBottom: 4 }}>超额</Button>
              <Button size="small" onClick={() => runBoundaryTest('remove_high_minout')} disabled={isProcessing || !canTest} style={{ fontSize: 12 }}>高minOut</Button>
            </div>

            <div style={{ padding: '8px', background: '#fff', borderRadius: '4px', border: '1px solid #e0e0e0' }}>
              <div style={{ fontSize: '11px', fontWeight: 'bold', color: '#333', marginBottom: '6px' }}>交换</div>
              <Button size="small" onClick={() => runBoundaryTest('swap_zero')} disabled={isProcessing || !canTest} style={{ fontSize: 12, marginRight: 4, marginBottom: 4 }}>金额=0</Button>
              <Button size="small" onClick={() => runBoundaryTest('swap_same_token')} disabled={isProcessing || !canTest} style={{ fontSize: 12, marginRight: 4, marginBottom: 4 }}>同币种</Button>
              <Button size="small" onClick={() => runBoundaryTest('swap_exceed_balance')} disabled={isProcessing || !canTest} style={{ fontSize: 12 }}>超额</Button>
            </div>
          </div>
        </div>
      )}
    </div>
  )
}
