import { useState, useEffect } from 'react';
import { config as configApi } from '../api';

export default function Output() {
  const [preview, setPreview] = useState('');
  const [loading, setLoading] = useState(false);
  const [copied, setCopied] = useState(false);
  const [surgeToken, setSurgeToken] = useState('');

  const surgeUrl = surgeToken ? `${window.location.origin}/surge/${surgeToken}` : '';

  const loadToken = async () => {
    try {
      const data = await configApi.getSurgeToken();
      setSurgeToken(data.token || '');
    } catch {}
  };

  const handleRegenerate = async () => {
    if (!confirm('重新生成后，旧的订阅链接将失效，Surge 客户端需要更新订阅地址。确定继续？')) return;
    try {
      const data = await configApi.regenerateSurgeToken();
      setSurgeToken(data.token);
      loadPreview();
    } catch {}
  };

  const loadPreview = async () => {
    setLoading(true);
    try {
      const data = await configApi.preview();
      setPreview(data.config);
    } catch (err: any) {
      setPreview(`# Error: ${err.message}`);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    loadToken();
    loadPreview();
  }, []);

  const handleCopy = () => {
    navigator.clipboard.writeText(surgeUrl);
    setCopied(true);
    setTimeout(() => setCopied(false), 2000);
  };

  return (
    <div>
      <h2 style={styles.title}>输出配置</h2>
      <p style={styles.subtitle}>Surge 客户端订阅链接和配置预览</p>

      {/* Subscription URL */}
      <div style={{
        background: 'var(--bg-input)',
        border: '1px solid var(--border)',
        borderRadius: 'var(--radius)',
        padding: 16,
        marginBottom: 20,
      }}>
        <div style={{
          fontSize: 10,
          fontFamily: 'var(--font-mono)',
          color: 'var(--text-muted)',
          textTransform: 'uppercase',
          letterSpacing: '0.1em',
          marginBottom: 8,
        }}>
          Surge 订阅链接
        </div>
        <div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
          <code style={{
            flex: 1,
            fontFamily: 'var(--font-mono)',
            fontSize: 13,
            color: 'var(--accent)',
            userSelect: 'all',
            overflow: 'hidden',
            textOverflow: 'ellipsis',
            whiteSpace: 'nowrap',
          }}>
            {surgeUrl || '加载中...'}
          </code>
          <button className="small" onClick={handleCopy} disabled={!surgeUrl}>
            {copied ? '已复制' : '复制'}
          </button>
          <button className="small" onClick={handleRegenerate} disabled={!surgeUrl}>
            重新生成
          </button>
        </div>
      </div>

      {/* Preview */}
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 8 }}>
        <span style={{
          fontSize: 10,
          fontFamily: 'var(--font-mono)',
          color: 'var(--text-muted)',
          textTransform: 'uppercase',
          letterSpacing: '0.1em',
        }}>
          配置预览
        </span>
        <button className="small" onClick={loadPreview} disabled={loading}>
          {loading ? '加载中...' : '刷新预览'}
        </button>
      </div>

      <pre style={{
        background: 'var(--bg-input)',
        border: '1px solid var(--border)',
        borderRadius: 'var(--radius)',
        padding: 16,
        fontFamily: 'var(--font-mono)',
        fontSize: 11,
        lineHeight: 1.6,
        color: 'var(--text-secondary)',
        overflow: 'auto',
        maxHeight: 'calc(100vh - 360px)',
        whiteSpace: 'pre-wrap',
        wordBreak: 'break-all',
      }}>
        {preview || '(empty)'}
      </pre>
    </div>
  );
}

const styles = {
  title: {
    fontFamily: 'var(--font-mono)' as const,
    fontSize: 16,
    fontWeight: 600 as const,
    color: 'var(--text-primary)',
    marginBottom: 4,
  },
  subtitle: {
    fontSize: 12,
    color: 'var(--text-secondary)',
    marginBottom: 20,
  },
};
