import { useState } from 'react';
import {
  ActivityIndicator,
  Image,
  Text,
  TouchableOpacity,
  View,
} from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { useTranslation } from 'react-i18next';
import {
  isValidDomain,
  normalizeDomain,
  type Tier,
} from '../../hooks/useCustomDomains';
import { useColors } from '../../lib/theme';
import { FormSheet } from '../FormSheet';
import { SheetFieldStack } from '../SheetFieldStack';

type Props = {
  visible: boolean;
  tier: Tier;
  onClose: () => void;
  onAdd: (domain: string) => Promise<{ ok: boolean; error?: string; alreadyGlobal?: boolean }>;
};

export function AddDomainSheet({ visible, tier, onClose, onAdd }: Props) {
  const { t } = useTranslation();
  const colors = useColors();
  const [input, setInput] = useState('');
  const [confirmPermanent, setConfirmPermanent] = useState(false);
  const [adding, setAdding] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [fieldsDone, setFieldsDone] = useState(false);

  const normalized = normalizeDomain(input);

  function close() {
    setInput('');
    setConfirmPermanent(false);
    setError(null);
    setFieldsDone(false);
    onClose();
  }

  async function handleAdd() {
    if (!isValidDomain(input) || !confirmPermanent || adding) return;
    setAdding(true);
    setError(null);
    const result = await onAdd(input);
    setAdding(false);
    if (result.ok) {
      close();
      return;
    }
    if (result.alreadyGlobal) {
      setError(t('blocker.add_sheet_already_global', { domain: normalized }));
    } else {
      setError(result.error ?? t('blocker.add_sheet_add_failed'));
    }
  }

  const warningText =
    tier.plan === 'free'
      ? t('blocker.add_sheet_warning_free')
      : t('blocker.add_sheet_warning_pro');

  return (
    <FormSheet
      visible={visible}
      onClose={close}
      title={t('blocker.add_sheet_title')}
      initialHeightPct={0.75}
      growWithKeyboard
    >
      <SheetFieldStack
        fields={[
          {
            key: 'domain',
            label: t('blocker.add_sheet_label'),
            placeholder: t('blocker.add_sheet_placeholder'),
            value: input,
            onChangeText: (v) => { setInput(v); setError(null); },
            normalize: normalizeDomain,
            keyboardType: 'url',
            autoCapitalize: 'none',
            autoCorrect: false,
            validate: (v) =>
              isValidDomain(v) ? undefined : t('blocker.add_sheet_invalid'),
          },
        ]}
        onComplete={() => setFieldsDone(true)}
      >
        {/* Favicon-Preview */}
        <View
          style={{
            flexDirection: 'row',
            alignItems: 'center',
            gap: 10,
            padding: 12,
            backgroundColor: colors.surfaceElevated,
            borderRadius: 12,
            marginBottom: 12,
          }}
        >
          <Image
            source={{
              uri: `https://www.google.com/s2/favicons?domain=${normalized}&sz=64`,
            }}
            style={{ width: 24, height: 24, borderRadius: 4 }}
          />
          <Text
            style={{
              flex: 1,
              fontSize: 14,
              fontFamily: 'Nunito_600SemiBold',
              color: colors.text,
            }}
            numberOfLines={1}
          >
            {normalized}
          </Text>
        </View>

        {/* Warnung */}
        <View
          style={{
            flexDirection: 'row',
            gap: 10,
            padding: 12,
            backgroundColor: '#fef3c7',
            borderRadius: 12,
            borderWidth: 1,
            borderColor: '#fcd34d',
            marginBottom: 12,
          }}
        >
          <Ionicons name="lock-closed" size={18} color="#92400e" />
          <Text
            style={{
              flex: 1,
              fontSize: 12,
              fontFamily: 'Nunito_400Regular',
              color: '#92400e',
              lineHeight: 17,
            }}
          >
            {warningText}
          </Text>
        </View>

        {/* Confirm-Checkbox */}
        <TouchableOpacity
          onPress={() => setConfirmPermanent((v) => !v)}
          activeOpacity={0.7}
          style={{
            flexDirection: 'row',
            alignItems: 'flex-start',
            gap: 10,
            paddingVertical: 4,
            marginBottom: 14,
          }}
        >
          <View
            style={{
              width: 22,
              height: 22,
              borderRadius: 6,
              borderWidth: 1.5,
              borderColor: confirmPermanent ? colors.success : colors.border,
              backgroundColor: confirmPermanent ? colors.success : colors.bg,
              alignItems: 'center',
              justifyContent: 'center',
              marginTop: 1,
            }}
          >
            {confirmPermanent && <Ionicons name="checkmark" size={16} color="#fff" />}
          </View>
          <Text
            style={{
              flex: 1,
              fontSize: 13,
              fontFamily: 'Nunito_400Regular',
              color: colors.text,
              lineHeight: 18,
            }}
          >
            {t('blocker.add_sheet_confirm_permanent')}
          </Text>
        </TouchableOpacity>

        {error && (
          <Text
            style={{
              fontSize: 13,
              fontFamily: 'Nunito_400Regular',
              color: '#dc2626',
              marginBottom: 10,
            }}
          >
            {error}
          </Text>
        )}

        {/* Add-Button */}
        <TouchableOpacity
          onPress={handleAdd}
          disabled={!confirmPermanent || adding}
          activeOpacity={0.85}
          style={{ marginBottom: 12 }}
        >
          <View
            style={{
              backgroundColor: !confirmPermanent ? '#d4d4d4' : '#dc2626',
              borderRadius: 14,
              paddingVertical: 14,
              alignItems: 'center',
            }}
          >
            {adding ? (
              <ActivityIndicator color="#fff" />
            ) : (
              <Text style={{ fontSize: 15, fontFamily: 'Nunito_700Bold', color: '#fff' }}>
                {t('blocker.add_sheet_title')}
              </Text>
            )}
          </View>
        </TouchableOpacity>
      </SheetFieldStack>
    </FormSheet>
  );
}
