import React, { useState, useEffect } from 'react'
import { Modal, Form, Input, Button, Alert, Space } from 'antd'
import { useSelector } from 'react-redux'
import type { RootState } from '../../store/store'

interface ConnectDialogProps {
  visible: boolean
  onConnect: (url: string) => void
  onCancel: () => void
}

/**
 * 连接服务器对话框
 */
const ConnectDialog: React.FC<ConnectDialogProps> = ({ 
  visible, 
  onConnect, 
  onCancel 
}) => {
  const [form] = Form.useForm()
  const [loading, setLoading] = useState(false)
  const { status: connectionStatus, serverUrl } = useSelector((state: RootState) => state.connection)

  // Preset server addresses
  const presetServers = [
    'http://localhost:3001',
    'http://127.0.0.1:3001',
    'http://192.168.1.100:3001',
  ]

  useEffect(() => {
    if (visible) {
      if (serverUrl) {
        form.setFieldValue('serverUrl', serverUrl)
      } else {
        form.setFieldValue('serverUrl', 'http://localhost:3001')
      }
    }
  }, [visible, serverUrl, form])

  useEffect(() => {
    if (connectionStatus === 'connected') {
      setLoading(false)
    } else if (connectionStatus === 'error') {
      setLoading(false)
    }
  }, [connectionStatus])

  const handleConnect = async () => {
    try {
      const values = await form.validateFields()
      setLoading(true)
      onConnect(values.serverUrl)
    } catch (error) {
      console.error('表单验证失败:', error)
    }
  }

  const handlePresetSelect = (url: string) => {
    form.setFieldValue('serverUrl', url)
  }

  const validateServerUrl = (_: any, value: string) => {
    if (!value) {
      return Promise.reject(new Error('请输入服务器地址'))
    }
    
    if (!value.startsWith('http://') && !value.startsWith('https://') &&
        !value.startsWith('ws://') && !value.startsWith('wss://')) {
      return Promise.reject(new Error('地址必须以 http:// 或 https:// 开头'))
    }
    
    try {
      // Normalize ws:// -> http:// for URL validation
      const normalized = value.replace(/^ws:\/\//, 'http://').replace(/^wss:\/\//, 'https://')
      new URL(normalized)
      return Promise.resolve()
    } catch {
      return Promise.reject(new Error('请输入有效的服务器地址'))
    }
  }

  return (
    <Modal
      title="连接到远程控制服务器"
      open={visible}
      onCancel={onCancel}
      width={500}
      footer={[
        <Button key="cancel" onClick={onCancel}>
          取消
        </Button>,
        <Button 
          key="connect" 
          type="primary" 
          loading={loading}
          onClick={handleConnect}
        >
          {loading ? '连接中...' : '连接'}
        </Button>,
      ]}
      maskClosable={false}
    >
      <Form
        form={form}
        layout="vertical"
        initialValues={{
          serverUrl: 'http://localhost:3001'
        }}
      >
        <Form.Item
          label="服务器地址"
          name="serverUrl"
          rules={[
            { validator: validateServerUrl }
          ]}
          help="输入服务器地址，例如: http://localhost:3001"
        >
          <Input 
            placeholder="http://localhost:3001"
            size="large"
            autoFocus
            onPressEnter={handleConnect}
          />
        </Form.Item>

        <div style={{ marginBottom: '16px' }}>
          <div style={{ marginBottom: '8px', color: '#666', fontSize: '14px' }}>
            常用地址：
          </div>
          <Space wrap>
            {presetServers.map((url) => (
              <Button
                key={url}
                size="small"
                type="dashed"
                onClick={() => handlePresetSelect(url)}
              >
                {url}
              </Button>
            ))}
          </Space>
        </div>

        {connectionStatus === 'error' && (
          <Alert
            message="连接失败"
            description="无法连接到服务器，请检查地址是否正确，服务器是否运行正常"
            type="error"
            showIcon
            style={{ marginBottom: '16px' }}
          />
        )}

        <Alert
          message="使用说明"
          description={
            <div>
              <p>1. 确保远程控制服务器已启动</p>
              <p>2. 如果服务器在本机，使用 localhost 或 127.0.0.1</p>
              <p>3. 如果服务器在局域网，使用服务器的IP地址</p>
              <p>4. 确保防火墙已开放相应端口</p>
            </div>
          }
          type="info"
          showIcon
        />
      </Form>
    </Modal>
  )
}

export default ConnectDialog 