import React, { Component } from "react";
import ReactDOM from "react-dom";

import {
  Header,
  Segment,
  Form,
  Input,
  Select,
  TextArea,
  Checkbox,
  Dropdown,
  Divider,
} from "semantic-ui-react";
import PropTypes from "prop-types";

export default class SupportFields extends Component {
  //props (expected component properties)
  static propTypes = {
    appState: PropTypes.object.isRequired,
    upState: PropTypes.object.isRequired,
    upStateUpdate: PropTypes.func.isRequired,

    fields: PropTypes.object.isRequired,
  };

  constructor(props) {
    super(props);

    this.devMode = props.appState.devMode;

    this.messageRefs = {};

    this.values = {};
    Object.values(props.fields).forEach((field) => {
      this.values[field.name] = field.value;
    });

    this.state = this.values;
  }

  txt = (text) => {
    //handle text translations from config
    const { props } = this;
    if (!props.appState.config) return text;
    if (text in props.appState.config.text)
      return props.appState.config.text[text];

    return text;
  };

  componentDidMount() {
    if (this.devMode)
      console.log("components/SupportForm componentDidMount", this.props);
  }

  onChange(event, data) {
    const value = data.type === "checkbox" ? data.checked : data.value;
    const name = data.name;

    //  if (this.devMode)
    //    console.log("Components/SupportFields onChange", name, value, data);

    if (name)
      this.setState({ [name]: value }, () =>
        this.props.upStateUpdate({
          fields: this.state,
        })
      );
  }

  componentDidUpdate() {}

  render() {
    const { props, state } = this;
    this.devMode = props.appState.devMode;

    var buffer = [];

    buffer.push(<Divider key="fieldDivider1" />);

    Object.values(props.fields).forEach((field) => {
      switch (field.type) {
        case "text":
          buffer.push(
            <Form.Field inline key={field.name}>
              <label>{field.name}</label>
              <Input
                name={field.name}
                fluid
                className={props.appState.className}
                placeholder={field.placeholder}
                value={state[field.name] ? state[field.name] : ""}
                onChange={(e, data) => this.onChange(e, data)}
              />
              <small>{field.instructions}</small>
            </Form.Field>
          );
          break;

        case "textarea":
          buffer.push(
            <Form.Field inline key={field.name}>
              <label>{field.name}</label>
              <TextArea
                name={field.name}
                fluid="true"
                className={props.appState.className}
                placeholder={field.placeholder}
                value={state[field.name]}
                onChange={(e, data) => this.onChange(e, data)}
              />
              <small>{field.instructions}</small>
            </Form.Field>
          );
          break;

        case "toggle":
          buffer.push(
            <Form.Field key={field.name}>
              <label>{field.name}</label>
              <Checkbox
                name={field.name}
                data-position="right center"
                toggle
                checked={state[field.name] ? true : false }
                onChange={(e, data) => this.onChange(e, data)}
              />
              <br />
              <small>{field.instructions}</small>
            </Form.Field>
          );
          break;

        case "select":
          buffer.push(
            <Form.Field inline key={field.name}>
              <label>{field.name}</label>
              <Dropdown
                name={field.name}
                selection
                compact
                fluid
                className={props.appState.className + " v-select"}
                data-position="right center"
                placeholder={field.placeholder}
                value={state[field.name]}
                options={field.options}
                onChange={(e, data) => this.onChange(e, data)}
              />
              <small>{field.instructions}</small>
            </Form.Field>
          );
          break;

        case "multiselect":
          buffer.push(
            <Form.Field inline key={field.name}>
              <label>{field.name}</label>
              <Dropdown
                name={field.name}
                multiple
                selection
                fluid
                className={props.appState.className + " v-select"}
                data-position="right center"
                placeholder={field.placeholder}
                value={Array.isArray(state[field.name]) ? state[field.name] : []}
                options={field.options}
                onChange={(e, data) => this.onChange(e, data)}
              />
              <small>{field.instructions}</small>
            </Form.Field>
          );
          break;

        default:
          buffer.push(
            <Form.Group key={field.name}>
              <label>{field.name}</label>
              <p> Unknown Field Type: {field.type}</p>
              <br />
              <small>{field.instructions}</small>
            </Form.Group>
          );
      }
    });
    buffer.push(<Divider key="fieldDivider2" />);

    return buffer;
  }
}
