import { useState } from 'react'
import { Button, Flex, Modal, Radio, Input, Form, Row, Col } from 'antd'
import { useDataFieldQuery } from '@/hooks/useQuery'
import AppProgressSteps from '@/components/AppProgressSteps'
import { useProcessData } from '../query'

export default function ButtonModal() {
  const { fieldOptions, getFieldName } = useDataFieldQuery()

  const [open, setOpen] = useState(false)
  const [current, setCurrent] = useState(0)
  const [form] = Form.useForm()

  const field = Form.useWatch('field', form)
  const action = Form.useWatch('action', form)
  const formFunction = Form.useWatch('function', form)
  const formSmFun = Form.useWatch('sm_fun', form)

  // 处理下一步按钮逻辑
  const handleNext = async () => {
    if (current === 0) {
      // 第一步验证表单
      await form.validateFields(['name', 'english_name', 'field'])
    } else if (current === 1) {
      await form.validateFields()
    }
    setCurrent(current + 1)
  }

  const { mutate, isPending, progress, message, remove } = useProcessData()

  // 开始处理
  const handleStart = () => {
    const values = form.getFieldsValue()
    const params = { ...values, rules: JSON.stringify(values.rules), action: '固定步长分箱' }
    mutate(params, {
      onSuccess: handleClose,
    })
  }

  // 关闭并重置
  const handleClose = () => {
    setOpen(false)
    setCurrent(0)
    remove()
  }
  // 步骤定义
  const steps = [
    {
      title: '请选择数据脱敏字段',
      content: (
        <>
          <Form.Item label="数据脱敏字段名称" name="name" rules={[{ required: true, message: '请输入字段名称' }]}>
            <Input placeholder="请输入" />
          </Form.Item>
          <Form.Item
            label="数据脱敏字段英文名称"
            name="english_name"
            rules={[{ required: true, message: '请输入英文名称' }]}>
            <Input placeholder="请输入" />
          </Form.Item>
          <Form.Item
            label="请选择数据脱敏字段"
            name="field"
            rules={[{ required: true, message: '请选择数据脱敏字段' }]}>
            <Radio.Group>
              <Row gutter={[10, 10]}>
                {fieldOptions.map((item) => (
                  <Col span={8} key={item.value}>
                    <Radio value={item.value}>{item.label}</Radio>
                  </Col>
                ))}
              </Row>
            </Radio.Group>
          </Form.Item>
        </>
      ),
    },
    {
      title: '配置数据脱敏规则',
      content: (
        <>
          <Form.Item label="数据脱敏操作字段">{getFieldName(field)}</Form.Item>
          <Form.Item name="action">
            <Radio.Group
              block
              options={['基础脱敏方法', '高级脱敏方法', '特殊脱敏方法']}
              optionType="button"
              buttonStyle="solid"
            />
          </Form.Item>
          {action === '基础脱敏方法' && (
            <>
              <Form.Item label="请选择脱敏方法" name="function" rules={[{ required: true, message: '请选择' }]}>
                <Radio.Group options={['固定遮蔽', '动态遮蔽', '内容替换', '内容模糊']} />
              </Form.Item>
              {formFunction === '固定遮蔽' && (
                <>
                  <Form.Item label="请输入遮蔽符号" name="char" rules={[{ required: true, message: '请输入' }]}>
                    <Input placeholder="请输入用于替换的固定符号，如：*、X等" />
                  </Form.Item>
                  <Form.Item label="请选择遮蔽范围" name="sm_fun" rules={[{ required: true, message: '请选择' }]}>
                    <Radio.Group options={['部分遮蔽', '全部遮蔽']} />
                  </Form.Item>
                  {formSmFun === '部分遮蔽' && (
                    <>
                      <Form.Item label="请输入开始位数" name="start" rules={[{ required: true, message: '请输入' }]}>
                        <Input placeholder="请输入" />
                      </Form.Item>
                      <Form.Item label="请输入结束位数" name="end" rules={[{ required: true, message: '请输入' }]}>
                        <Input placeholder="请输入" />
                      </Form.Item>
                    </>
                  )}
                </>
              )}

              {formFunction === '动态遮蔽' && (
                <>
                  <Form.Item label="请输入遮蔽符号" name="char" rules={[{ required: true, message: '请输入' }]}>
                    <Input placeholder="请输入用于替换的固定符号，如：*、X等" />
                  </Form.Item>
                  <Form.Item label="请输入开始动态位数" name="start" rules={[{ required: true, message: '请输入' }]}>
                    <Input placeholder="请输入" />
                  </Form.Item>
                  <Form.Item label="请输入结束动态位数" name="end" rules={[{ required: true, message: '请输入' }]}>
                    <Input placeholder="请输入" />
                  </Form.Item>
                </>
              )}

              {formFunction === '内容替换' && (
                <>
                  <Form.Item label="请输入替换内容" name="char" rules={[{ required: true, message: '请输入' }]}>
                    <Input placeholder="请输入" />
                  </Form.Item>
                </>
              )}
              {formFunction === '内容模糊' && (
                <>
                  <Form.Item label="请输入模糊内容" name="char" rules={[{ required: true, message: '请输入' }]}>
                    <Input placeholder="请输入" />
                  </Form.Item>
                </>
              )}
            </>
          )}
          {action === '高级脱敏方法' && (
            <>
              <Form.Item label="请选择脱敏方法" name="function" rules={[{ required: true, message: '请选择' }]}>
                <Radio.Group options={['伪名化', '数据扰动', '空值化', '数据组合']} />
              </Form.Item>
              {formFunction === '伪名化' && (
                <>
                  <Form.Item label="请选择伪名化方法" name="sm_fun" rules={[{ required: true, message: '请选择' }]}>
                    <Radio.Group options={['AI智能']} />
                  </Form.Item>
                </>
              )}
              {formFunction === '数据扰动' && (
                <>
                  <Form.Item label="请选择伪名化方法" name="sm_fun" rules={[{ required: true, message: '请选择' }]}>
                    <Radio.Group options={['AI智能']} />
                  </Form.Item>
                </>
              )}
              {formFunction === '数据组合' && (
                <>
                  <Form.Item label="请选择数据组合方法" name="sm_fun" rules={[{ required: true, message: '请选择' }]}>
                    <Radio.Group options={['添加前缀', '添加后缀']} />
                  </Form.Item>
                  <Form.Item name="char" rules={[{ required: true, message: '请输入' }]}>
                    <Input placeholder="请输入" />
                  </Form.Item>
                </>
              )}
            </>
          )}
          {action === '特殊脱敏方法' && (
            <>
              <Form.Item label="请选择脱敏方法" name="function" rules={[{ required: true, message: '请选择' }]}>
                <Radio.Group options={['数据加密']} />
              </Form.Item>
              <Form.Item label="请选择数据加密方法" name="sm_fun" rules={[{ required: true, message: '请选择' }]}>
                <Radio.Group options={['AES', 'RSA']} />
              </Form.Item>
            </>
          )}
        </>
      ),
    },
    {
      title: '处理执行',
      content: (
        <>
          <Flex vertical align="center" style={{ marginTop: '20px' }}>
            <Button type="primary" onClick={handleStart} loading={isPending}>
              开始处理
            </Button>
            <AppProgressSteps
              current={progress}
              items={[
                { title: '新建字段' },
                { title: '复制数据' },
                { title: '数据脱敏处理' },
                { title: '处理结果', description: <>{message[3]}</> },
              ]}
            />
          </Flex>
        </>
      ),
    },
  ]

  return (
    <>
      <Button type="primary" onClick={() => setOpen(true)}>
        数据脱敏
      </Button>
      <Modal
        title={steps[current]?.title}
        open={open}
        footer={
          <Flex justify="center" gap={20}>
            {current === 1 && <Button type="primary">AI智能建议</Button>}
            {current > 0 && <Button onClick={() => setCurrent(current - 1)}>上一步</Button>}
            {current < steps.length - 1 && (
              <Button type="primary" onClick={handleNext}>
                下一步
              </Button>
            )}
            {current === steps.length - 1 && (
              <Button type="primary" onClick={handleClose} disabled={isPending}>
                关闭
              </Button>
            )}
          </Flex>
        }
        destroyOnClose
        width={800}
        onCancel={handleClose}>
        <div style={{ minHeight: 300, padding: '20px 0' }}>
          <Form form={form} labelCol={{ span: 5 }} preserve={false} initialValues={{ action: '基础脱敏方法' }}>
            {steps.map((item, index) => (
              <div key={index} hidden={current !== index} style={{ marginTop: '20px' }}>
                {item.content}
              </div>
            ))}
          </Form>
        </div>
      </Modal>
    </>
  )
}
