import React from 'react';
import { View, Text, StyleSheet } from 'react-native';

import { GradientCard } from './GradientCard';
import { useTheme } from '../theme/context';
import type { PointBalance } from '../types';

interface PointsBalanceProps {
  balances: PointBalance[];
  style?: object;
}

export function PointsBalance({ balances, style }: PointsBalanceProps) {
  const theme = useTheme();

  // Find primary and secondary currencies
  const primary = balances.find((b) => b.currency === 'qwik-coins') || balances[0];
  const secondary = balances.find((b) => b.currency !== primary?.currency);

  const formattedBalance = primary?.balance.toLocaleString() || '0';
  const secondaryBalance = secondary
    ? `${secondary.balance.toLocaleString()} ${secondary.currency}`
    : null;

  return (
    <GradientCard style={[styles.card, style]}>
      <View style={styles.header}>
        <Text style={styles.label}>CURRENT BALANCE</Text>
        <Text style={styles.brand}>QwikCard</Text>
      </View>

      <View style={styles.mainContent}>
        <Text style={styles.currencySymbol}>$</Text>
        <Text style={styles.amount}>{formattedBalance}</Text>
      </View>

      <View style={styles.footer}>
        {secondaryBalance && (
          <View style={styles.chip}>
            <Text style={styles.chipText}>+ {secondaryBalance}</Text>
          </View>
        )}
        <View style={styles.dots}>
          <View style={[styles.dot, { backgroundColor: '#FFD700' }]} />
          <View style={[styles.dot, { backgroundColor: '#FF69B4' }]} />
        </View>
      </View>
    </GradientCard>
  );
}

const styles = StyleSheet.create({
  card: {
    padding: 20,
    minHeight: 150,
    justifyContent: 'space-between',
  },
  header: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    marginBottom: 16,
  },
  label: {
    color: 'rgba(255, 255, 255, 0.8)',
    fontSize: 10,
    fontWeight: '700',
    letterSpacing: 1.2,
  },
  brand: {
    color: '#FFFFFF',
    fontSize: 12,
    fontWeight: '800',
    fontStyle: 'italic',
  },
  mainContent: {
    flexDirection: 'row',
    alignItems: 'flex-start',
  },
  currencySymbol: {
    color: 'rgba(255, 255, 255, 0.9)',
    fontSize: 20,
    fontWeight: '600',
    marginTop: 2,
    marginRight: 4,
  },
  amount: {
    color: '#FFFFFF',
    fontSize: 32,
    fontWeight: '800',
    letterSpacing: -0.5,
  },
  footer: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    marginTop: 16,
  },
  chip: {
    backgroundColor: 'rgba(255, 255, 255, 0.2)',
    paddingHorizontal: 10,
    paddingVertical: 4,
    borderRadius: 16,
  },
  chipText: {
    color: '#FFFFFF',
    fontSize: 11,
    fontWeight: '600',
  },
  dots: {
    flexDirection: 'row',
    gap: 5,
  },
  dot: {
    width: 20,
    height: 20,
    borderRadius: 10,
    opacity: 0.8,
  },
});

export default PointsBalance;
