import React, { createContext, ReactNode, useCallback, useContext, useEffect } from 'react'
import { useAccount, useReadContract, useWriteContract } from 'wagmi'
import contractAbi from "../../../out/RaidGeld.sol/RaidGeld.json"
import { parseEther } from 'viem'
import contractAddress from '../../contract_address'

const abi = contractAbi.abi

export type UnitType = 0 | 1 | 2 | 3

export interface Player {
  created_at: bigint,
  last_raided_at: bigint,
  total_minted: bigint
}
export interface Army {
  anointed: { level: number }
  apprentice: { level: number }
  champion: { level: number }
  moloch_denier: { level: number }
  profit_per_second: bigint
}

export interface PlayerContextType {
  isRegistered: boolean,
  player: null | Player,
  army: null | Army,
  balance: bigint,
  register: () => void,
  raid: () => void,
  addUnit: (unit: UnitType) => void
}

const PlayerContext = createContext<PlayerContextType>({
  isRegistered: false,
  player: null,
  army: null,
  balance: BigInt(0),
  register: () => { },
  raid: () => { },
  addUnit: () => { }
});

const PlayerProvider = ({ children }: { children: ReactNode }) => {
  const { address, isConnected } = useAccount();
  const { writeContract, error } = useWriteContract();

  useEffect(() => {
    console.warn(error)
  }, [error])

  const { data: isRegistered } = useReadContract({
    address: contractAddress,
    abi,
    functionName: 'isRegistered',
    args: [address],
    query: {
      enabled: isConnected,
      refetchInterval: 5,
    }
  });

  const { data: balance, } = useReadContract({
    address: contractAddress,
    abi,
    functionName: 'balanceOf',
    args: [address],
    query: {
      refetchInterval: 5,
      enabled: isConnected
    }
  });

  const { data: player } = useReadContract({
    address: contractAddress,
    abi,
    functionName: 'getPlayer',
    args: [address],
    query: {
      enabled: isConnected,
      refetchInterval: 15
    }
  });

  const { data: army } = useReadContract({
    address: contractAddress,
    abi,
    functionName: 'getArmy',
    args: [address],
    query: {
      enabled: isConnected,
      refetchInterval: 15
    }
  });

  console.log(balance, player, army)

  const register = useCallback(() => {
    writeContract({
      abi,
      address: contractAddress,
      functionName: 'register',
      value: parseEther("0.00005"),
    })
  }, [writeContract])

  const raid = useCallback(() => {
    writeContract({
      abi,
      address: contractAddress,
      functionName: 'raid',
    })
  }, [writeContract])

  const addUnit = useCallback((unit: UnitType) => {
    writeContract({
      abi,
      address: contractAddress,
      functionName: 'addUnit',
      args: [unit, 1]
    })
  }, [writeContract])

  return (
    <PlayerContext.Provider value={{
      isRegistered: isRegistered as boolean,
      player: player as Player,
      army: army as Army,
      balance: balance as bigint,
      register,
      raid,
      addUnit
    }}>
      {children}
    </PlayerContext.Provider>
  );
}

export const usePlayer = () => {
  return useContext(PlayerContext);
}

export default PlayerProvider

