import React, { useState } from 'react'
import { toast } from 'react-toastify'
import { SettingsClient } from '../../../generated/ManagingApi'
import useApiUrlStore from '../../../app/store/apiStore'
import { GmxSdk } from '@gmx-io/sdk'
import { createClient, createTestClient, createWalletClient, http, publicActions, walletActions } from 'viem'
import { getClient, getConnectorClient } from '@wagmi/core'
import { privyWagmiConfig } from '../../../config/privy'
import { arbitrum } from 'viem/chains'
import { GmxSdkConfig } from '@gmx-io/sdk/build/src/types/sdk'

const DefaultConfig: React.FC = () => {
  const [isCreating, setIsCreating] = useState(false)
  const { apiUrl } = useApiUrlStore()

  const client = createWalletClient({
    account: "0x9f7198eb1b9Ccc0Eb7A07eD228d8FbC12963ea33",
    chain: arbitrum,
    transport: http(),
  })

  const testClient = createTestClient({
    chain: arbitrum,
    mode: "hardhat",
    transport: http(),
  })
    .extend(publicActions)
    .extend(walletActions);
  

    const arbitrumSdkConfig: GmxSdkConfig = {
      chainId: arbitrum.id,
      account: "0x9f7198eb1b9Ccc0Eb7A07eD228d8FbC12963ea33",
      oracleUrl: "https://arbitrum-api.gmxinfra.io",
      rpcUrl: "https://arb1.arbitrum.io/rpc",
      walletClient: client,
      subsquidUrl: "https://gmx.squids.live/gmx-synthetics-arbitrum:live/api/graphql",
      subgraphUrl: "https://subgraph.satsuma-prod.com/3b2ced13c8d9/gmx/synthetics-arbitrum-stats/api",
    };

    const sdk = new GmxSdk(arbitrumSdkConfig)

  console.log(sdk)
  
  const createDefaultConfig = async () => {
    try {
      const client = new SettingsClient({}, apiUrl)
      setIsCreating(true)
      const response = await client.settings_CreateDefaultConfiguration()
        .then((response) => {
          if (response) {
            toast.success('Default configuration created successfully!')
          } else {
            toast.error(`Failed to create default configuration`)
          }
        })
    } catch (error) {
      toast.error(`Error: ${error instanceof Error ? error.message : String(error)}`)
    } finally {
      setIsCreating(false)
    }
  }

  return (
    <div className="max-w-3xl mx-auto p-4">
      <div className="card bg-base-100 shadow-xl p-6">
        <h2 className="text-2xl font-bold mb-4">Default Configuration</h2>
        
        <div className="mb-6">
          <p className="mb-2">Create a default configuration to get started quickly with backtesting:</p>
          
          <div className="bg-base-200 p-4 rounded-lg mb-4">
            <h3 className="text-lg font-semibold mb-2">Money Management:</h3>
            <ul className="list-disc pl-6 mb-2">
              <li>Name: Default Hourly</li>
              <li>Timeframe: 1 Hour</li>
              <li>Balance at Risk: 25%</li>
              <li>Stop Loss: 2%</li>
              <li>Take Profit: 4%</li>
              <li>Leverage: 1x</li>
            </ul>
            
            <h3 className="text-lg font-semibold mb-2">Strategy:</h3>
            <ul className="list-disc pl-6 mb-2">
              <li>Type: StcTrend</li>
              <li>Name: Default StcTrend</li>
              <li>SignalType: Both</li>
              <li>FastPeriods: 23</li>
              <li>SlowPeriods: 50</li>
              <li>StochPeriods: 10</li>
            </ul>
            
            <h3 className="text-lg font-semibold mb-2">Scenario:</h3>
            <ul className="list-disc pl-6">
              <li>Name: Default Scenario</li>
              <li>Contains: Default StcTrend strategy</li>
            </ul>
          </div>
        </div>
        
        <div className="flex justify-center">
          <button 
            onClick={createDefaultConfig}
            disabled={isCreating}
            className="btn btn-primary"
          >
            {isCreating ? 'Creating...' : 'Create Default Configuration'}
          </button>
        </div>
      </div>
    </div>
  )
}

export default DefaultConfig 
