import React, { useEffect, useState } from 'react';
import { Form, Input, DatePicker, Select, Button, message, FormInstance } from 'antd';
import moment from 'moment';
import { useNavigate } from 'react-router-dom';
import { GetMachineAuthorizationTypeOptions } from '@/services/enum/machineAuthorizationEnum';
const { TextArea } = Input;
import CryptoJS from 'crypto-js'; // 添加这一行导入
import * as LZString from 'lz-string';
import { request } from '@umijs/max';


interface AddMachineIdAuthorizationProps {
  setFormRef: (form: FormInstance) => void;
  id: string;
}

const AddMachineIdAuthorization: React.FC<AddMachineIdAuthorizationProps> = ({ setFormRef, id }) => {
  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);
  const [messageApi, messageHolder] = message.useMessage();

  useEffect(() => {
    setFormRef(form);
    form.setFieldsValue({
      authorizedDate: moment(),
      expiryDate: moment().add(1, 'year'),
      type: 0,
      authorizationCode: '123'
    })
  }, [form, setFormRef, id]);

  const handleSubmit = async () => {
    try {
      setLoading(true);
      // TODO: Implement API call to save the authorization
      let values = form.getFieldsValue();
      console.log('Form values:', values);

      let res = await request<ApiResponse.SuccessItem<string>>('lms/Other/AddMachineAuthorization', {
        method: "POST",
        data: {
          ...values,
          authorizedDate: values.authorizedDate.toISOString(),
          expiryDate: values.expiryDate.toISOString()
        }
      })
      if (res.code !== 1) {
        throw new Error(res.message);
      }
      messageApi.success('Machine ID authorization added successfully');
    } catch (error : any) {
      messageApi.error(error.message);
    } finally {
      setLoading(false);
    }
  };


  function GenerateAuthorizationCode() {
    const values = form.getFieldsValue();
    const { machineId, type, authorizedDate, expiryDate } = values;

    if (!machineId || type === undefined || !authorizedDate || !expiryDate) {
      messageApi.error('请先填写必要信息（机器码、类型和日期）');
      return;
    }
    try {
      // Format dates to strings
      const authDate = moment(authorizedDate).format('YYYY-MM-DD HH:mm:ss');
      const expDate = moment(expiryDate).format('YYYY-MM-DD HH:mm:ss');
      let obj = {
        machineId: machineId,
        type: type,
        authorizedDate: authDate,
        expiryDate: expDate
      }

      // Create the string to encrypt
      const dataToEncrypt = JSON.stringify(obj);

      // Assuming CryptoJS is imported
      const secretKey = machineId;

      // Generate a secure encryption key from machineId
      const key = CryptoJS.enc.Utf8.parse(CryptoJS.SHA256(secretKey).toString());

      // Generate a random initialization vector
      const iv = CryptoJS.lib.WordArray.random(16);

      // Encrypt the data using AES encryption
      const encrypted = CryptoJS.AES.encrypt(dataToEncrypt, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      });

      // Convert IV to base64 for storage
      const ivBase64 = CryptoJS.enc.Base64.stringify(iv);

      // Get the encrypted data in base64 format
      const encryptedBase64 = encrypted.toString();

      // Combine IV and encrypted data with a delimiter for future decryption
      const authCode = ivBase64 + ':' + encryptedBase64;

      // 使用LZString压缩
      const compressedCode = LZString.compressToEncodedURIComponent(authCode);
      // Set the encrypted value in the form
      form.setFieldsValue({ authorizationCode: compressedCode });

      messageApi.success('授权码已生成');
    } catch (error) {
      console.error('生成授权码时出错:', error);
      messageApi.error('生成授权码失败');
    }
  }

  function DecryptAuthorizationCode(authCode: string, machineId: string) {
    try {
      // 解压缩
      const originalAuthCode = LZString.decompressFromEncodedURIComponent(authCode);
      // 拆分授权码，获取IV和加密数据
      const [ivBase64, encryptedBase64] = originalAuthCode.split(':');

      if (!ivBase64 || !encryptedBase64) {
        throw new Error('无效的授权码格式');
      }

      // 从Base64转换回IV
      const iv = CryptoJS.enc.Base64.parse(ivBase64);

      // 使用相同的方法生成密钥
      const secretKey = machineId;
      const key = CryptoJS.enc.Utf8.parse(CryptoJS.SHA256(secretKey).toString());

      // 解密数据
      const decrypted = CryptoJS.AES.decrypt(encryptedBase64, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      });

      // 将解密后的数据转换为字符串
      const decryptedData = decrypted.toString(CryptoJS.enc.Utf8);

      // 将JSON字符串解析为对象
      const decodedObject = JSON.parse(decryptedData);

      return {
        success: true,
        data: decodedObject
      };
    } catch (error) {
      console.error('解密授权码时出错:', error);
      return {
        success: false,
        error: error instanceof Error ? error.message : '未知错误'
      };
    }
  }


  return (
    <Form
      form={form}
      layout="vertical"
      onFinish={handleSubmit}
      autoComplete="off"
    >
      <Form.Item
        name="machineId"
        label="机器码/唯一授权码"
        rules={[{ required: true, message: '请输入 machine ID' }]}
      >
        <Input placeholder="输入 machine ID" />
      </Form.Item>

      <Form.Item
        name="type"
        label="授权软件类型"
        rules={[{ required: true, message: '请输入授权软件类型' }]}
      >
        <Select placeholder="选择授权软件类型" options={GetMachineAuthorizationTypeOptions()}></Select>
      </Form.Item>

      <Form.Item
        name="authorizedDate"
        label="授权时间"
        rules={[{ required: true, message: '请选择授权时间' }]}
      >
        <DatePicker
          showTime
          style={{ width: '100%' }}
          placeholder="选择授权日期和时间"
          format="YYYY-MM-DD HH:mm:ss"
        />
      </Form.Item>

      <Form.Item
        name="expiryDate"
        label="到期时间"
        rules={[{ required: true, message: '请选择到期时间' }]}
      >
        <DatePicker
          showTime
          style={{ width: '100%' }}
          placeholder="选择到期日期和时间"
          format="YYYY-MM-DD HH:mm:ss"
        />
      </Form.Item>

      <Form.Item name="authorizationCode" label="请输入授权码">
        <TextArea rows={4} placeholder="请输入授权码" />
      </Form.Item>

      <Form.Item style={{ textAlign: 'right' }}>
        <Button
          style={{ marginRight: 8 }}
          type="primary"
          onClick={GenerateAuthorizationCode}
        >
          计算授权码
        </Button>
        <Button
          style={{ marginRight: 8 }}
          type="primary"
          onClick={() => {
            const values = form.getFieldsValue();
            const { machineId, authorizationCode } = values;
            if (!machineId || !authorizationCode) {
              messageApi.error('请先填写机器码和授权码');
              return;
            }
            const result = DecryptAuthorizationCode(authorizationCode, machineId);
            if (result.success) {
              messageApi.success('授权码解密成功');
              console.log('解密结果:', result.data);
            } else {
              messageApi.error(`解密失败: ${result.error}`);
            }
          }}
        >
          解密授权码
        </Button>

        <Button type="primary" htmlType="submit" loading={loading}>
          保存
        </Button>
      </Form.Item>
      {messageHolder}
    </Form >
  );
};

export default AddMachineIdAuthorization;