import React from 'react'
import { useGameStore } from '../store/useGameStore'
import { UPGRADES } from '../constants'
import { styled } from '@linaria/react'
import type { UpgradeCost, Upgrade } from '../types'

const TempleContainer = styled.div`
  padding: 1rem;
`

const Title = styled.h2`
  font-size: 1.5rem;
  font-weight: bold;
  margin-bottom: 1rem;
  text-align: center;
`

const PrayerSection = styled.div`
  background-color: #f8fafc;
  border: 2px solid #e2e8f0;
  border-radius: 0.75rem;
  padding: 1.5rem;
  margin-bottom: 2rem;
  text-align: center;
`

const PietyDisplay = styled.div`
  font-size: 1.25rem;
  color: #4b5563;
  margin-bottom: 1rem;
`

const PrayerButton = styled.button<{ disabled?: boolean }>`
  padding: 1rem 2rem;
  font-size: 1.25rem;
  border-radius: 0.5rem;
  background-color: ${(props) => (props.disabled ? '#9ca3af' : '#8b5cf6')};
  color: white;
  font-weight: bold;
  cursor: ${(props) => (props.disabled ? 'not-allowed' : 'pointer')};
  border: none;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0 auto;

  &:hover:not(:disabled) {
    background-color: #7c3aed;
    transform: translateY(-1px);
  }

  &:active:not(:disabled) {
    transform: translateY(0);
  }
`

const UpgradesGrid = styled.div`
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 1rem;
  margin-top: 1rem;
`

const UpgradeCard = styled.div<{ purchased?: boolean }>`
  background-color: ${(props) => (props.purchased ? '#e5e7eb' : '#f3f4f6')};
  border: 1px solid #d1d5db;
  border-radius: 0.5rem;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
`

const UpgradeName = styled.h3`
  font-size: 1.25rem;
  font-weight: 500;
  margin: 0;
`

const UpgradeDescription = styled.p`
  color: #4b5563;
  margin: 0;
`

const UpgradeCost = styled.div`
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin-top: 0.5rem;
`

const CostItem = styled.div`
  display: flex;
  align-items: center;
  gap: 0.5rem;
`

const PurchaseButton = styled.button<{ disabled?: boolean }>`
  padding: 0.5rem 1rem;
  border-radius: 0.25rem;
  background-color: ${(props) => (props.disabled ? '#9ca3af' : '#3b82f6')};
  color: white;
  font-weight: bold;
  cursor: ${(props) => (props.disabled ? 'not-allowed' : 'pointer')};
  border: none;
  margin-top: auto;
`

const TempleComponent: React.FC = () => {
  const {
    inventory,
    purchasedUpgrades,
    purchaseUpgrade,
    pray,
    piety,
    actionCooldown,
  } = useGameStore()

  const handlePurchase = (upgradeId: string) => {
    purchaseUpgrade(upgradeId)
  }

  const canAffordUpgrade = (costs: UpgradeCost[]) => {
    return costs.every(
      (cost) =>
        inventory[cost.itemId] !== undefined &&
        inventory[cost.itemId] >= cost.amount,
    )
  }

  const canPurchaseUpgrade = (upgrade: Upgrade) => {
    // Check if already purchased
    if (purchasedUpgrades.includes(upgrade.id)) {
      return false
    }

    // Check if can afford
    if (!canAffordUpgrade(upgrade.cost)) {
      return false
    }

    // Check requirements
    if (
      upgrade.id === 'aqueous_vigor_2' &&
      !purchasedUpgrades.includes('aqueous_vigor_1')
    ) {
      return false
    }

    if (
      upgrade.id === 'aqua_diffundere_2' &&
      !purchasedUpgrades.includes('aqua_diffundere_1')
    ) {
      return false
    }

    return true
  }

  const getUpgradeStatus = (upgrade: Upgrade) => {
    if (purchasedUpgrades.includes(upgrade.id)) {
      return 'Purchased'
    }
    if (
      upgrade.id === 'aqueous_vigor_2' &&
      !purchasedUpgrades.includes('aqueous_vigor_1')
    ) {
      return 'Requires Aqueous Vigor I'
    }
    if (
      upgrade.id === 'aqua_diffundere_2' &&
      !purchasedUpgrades.includes('aqua_diffundere_1')
    ) {
      return 'Requires Aqua Diffundere I'
    }
    if (!canAffordUpgrade(upgrade.cost)) {
      return 'Cannot Afford'
    }
    return 'Purchase'
  }

  return (
    <TempleContainer>
      <Title>Temple</Title>

      <PrayerSection>
        <PietyDisplay>Piety Level: {piety}</PietyDisplay>
        <PrayerButton onClick={() => pray()} disabled={actionCooldown > 0}>
          🙏 Pray to the Gods
          {actionCooldown > 0 && ` (${(actionCooldown / 1000).toFixed(1)}s)`}
        </PrayerButton>
      </PrayerSection>

      <UpgradesGrid>
        {Object.values(UPGRADES).map((upgrade) => {
          const isPurchased = purchasedUpgrades.includes(upgrade.id)
          const canPurchase = canPurchaseUpgrade(upgrade)
          const status = getUpgradeStatus(upgrade)

          return (
            <UpgradeCard key={upgrade.id} purchased={isPurchased}>
              <UpgradeName>{upgrade.name}</UpgradeName>
              <UpgradeDescription>{upgrade.description}</UpgradeDescription>
              <UpgradeCost>
                <span>Cost:</span>
                {upgrade.cost.map((cost, index) => (
                  <CostItem key={index}>
                    <span>
                      {cost.amount} {cost.itemId}
                    </span>
                    <span>(You have: {inventory[cost.itemId] || 0})</span>
                  </CostItem>
                ))}
              </UpgradeCost>
              <PurchaseButton
                onClick={() => handlePurchase(upgrade.id)}
                disabled={!canPurchase}
              >
                {status}
              </PurchaseButton>
            </UpgradeCard>
          )
        })}
      </UpgradesGrid>
    </TempleContainer>
  )
}

export default TempleComponent
