import React from 'react';
import { Form, Checkbox, Input, Row, Col } from 'antd';

import { StateInfo, PointLightStateConfig } from '@easytwin/core';
import { SliderInput, ColorInput } from '@/components';

export interface PointLightFormProp {
  dataSource: StateInfo['config'];
  onChange: (data: PointLightStateConfig) => void;
}

type FormValuesChangeType = PointLightStateConfig;

const PointLightForm: React.FC<PointLightFormProp> = function ({ dataSource, onChange }) {
  const [form] = Form.useForm();

  React.useEffect(() => {
    form.setFieldsValue(dataSource);
  }, [form, dataSource]);

  return (
    <Form
      form={form}
      className="!px-1"
      labelCol={{
        span: 6,
      }}
      wrapperCol={{
        span: 18,
      }}
      labelAlign="left"
      colon={false}
      onValuesChange={(changedValue: FormValuesChangeType) => {
        onChange({
          ...dataSource,
          ...changedValue,
        });
      }}>
      {/* position */}
      <Form.Item label="光源位置" className="!mb-2">
        <Input.Group size="small">
          <Row gutter={8}>
            <Col span={8}>
              <Form.Item name="positionX" className="!mb-0">
                <Input size="small" suffix="X" />
              </Form.Item>
            </Col>
            <Col span={8}>
              <Form.Item name="positionY" className="!mb-0">
                <Input size="small" suffix="Y" />
              </Form.Item>
            </Col>
            <Col span={8}>
              <Form.Item name="positionZ" className="!mb-0">
                <Input size="small" suffix="Z" />
              </Form.Item>
            </Col>
          </Row>
        </Input.Group>
      </Form.Item>

      {/* 颜色 */}
      <Form.Item name="color" label="颜色" className="!mb-2">
        <ColorInput />
      </Form.Item>

      {/* 强度 */}
      <Form.Item name="intensity" label="强度" className="!mb-2">
        <SliderInput min={0.1} max={2} step={0.1} />
      </Form.Item>

      {/* distance */}
      <Form.Item name="decay" label="衰减半径" className="!mb-2">
        <SliderInput min={1} max={10} step={1} />
      </Form.Item>

      {/* shadow */}
      <Form.Item name="castShadow" label="投射阴影" valuePropName="checked" className="!mb-2">
        <Checkbox />
      </Form.Item>
    </Form>
  );
};

export default PointLightForm;
