import React, { Component } from 'react';
import { Link } from 'react-router';

import Uploadcare from 'cem/components/uploadcare';

import { reduxForm } from 'redux-form';
import { formFields } from 'cem/constants/contacts';
import validate from 'cem/validators/contacts';

import cn from 'classnames';
import styles from 'cem/styles/id/header';

import UI from 'cem/components/ui';
const { Grid, Form, Button, Media, Icon } = UI;

// Components
const Image = ({ photo, fields }) => (
  <div>
    <div className={styles.imageWrapper}>
      {photo && <UI.Image src={photo.url} kind="circle" width="102" height="102" />}
      <Form.Input type="file" className={styles.file} {...fields.photo} value={null} />
    </div>
    <Uploadcare {...fields.photo} />
  </div>
);

const Description = (props) => {
  const {
    fields,
    // values,
  } = props;

  return (
    <section>
      <Grid.Row>
        <Grid.Col sm="6" md="5">
          <Form.Group float kind={fields.details.lastName.touched && !!fields.details.lastName.error && `error`}>
            <Form.Input className={styles.input} block type="text" placeholder="Фамилия" {...fields.details.lastName} required />
            <Form.Label>Фамилия</Form.Label>
            {fields.details.lastName.touched && fields.details.lastName.error && <Form.Helper>{fields.details.lastName.error}</Form.Helper>}
          </Form.Group>
        </Grid.Col>
        <Grid.Col sm="6" md="5">
          <Form.Group float kind={fields.details.firstName.touched && !!fields.details.firstName.error && `error`}>
            <Form.Input className={styles.input} block type="text" placeholder="Имя" {...fields.details.firstName} required />
            <Form.Label>Имя</Form.Label>
            {fields.details.firstName.touched && fields.details.firstName.error && <Form.Helper>{fields.details.firstName.error}</Form.Helper>}
          </Form.Group>
        </Grid.Col>
        <Grid.Col sm="6" md="5">
          <Form.Group float kind={fields.details.middleName.touched && !!fields.details.middleName.error && `error`}>
            <Form.Input className={styles.input} block type="text" placeholder="Отчество" {...fields.details.middleName} required />
            <Form.Label>Отчество</Form.Label>
            {fields.details.middleName.touched && fields.details.middleName.error && <Form.Helper>{fields.details.middleName.error}</Form.Helper>}
          </Form.Group>
        </Grid.Col>
      </Grid.Row>
      <Grid.Row>
        <Grid.Col sm="10" md="5">
          <Form.Group>
            <Media
              left={<Icon icon="facebook" className={cn(styles.iconSocial, styles.facebook)} />}
              body={<Form.Input block type="text" {...fields.details.facebook} />}
            />
          </Form.Group>
        </Grid.Col>
        <Grid.Col sm="10" md="5">
          <Form.Group>
            <Media
              left={<Icon icon="twitter" className={styles.iconSocial} />}
              body={<Form.Input block type="text" {...fields.details.twitter} />}
            />
          </Form.Group>
        </Grid.Col>
        <Grid.Col sm="10" md="5">
          <Form.Group>
            <Media
              left={<Icon icon="instagram" className={styles.iconSocial} />}
              body={<Form.Input block type="text" {...fields.details.instagram} />}
            />
          </Form.Group>
        </Grid.Col>
        <Grid.Col sm="10" md="5">
          <Form.Group>
            <Media
              left={<Icon icon="vk" className={styles.iconSocial} />}
              body={<Form.Input block type="text" {...fields.details.vk} />}
            />
          </Form.Group>
        </Grid.Col>
      </Grid.Row>
    </section>
  );
};

class Header extends Component {
  createOrUpdate() {
    const { formKey, actions, values } = this.props;

    if (formKey === `create`) {
      actions.createId(values);
    } else {
      actions.updateId(values);
    }
  }

  render() {
    const {
      handleSubmit,
      formKey,
      fields,
      pristine,
    } = this.props;

    return (
      <Form.Container onSubmit={handleSubmit(::this.createOrUpdate)} className={styles.header}>
        <Grid.Container fluid>
          <Grid.Row>
            <Grid.Col xs="20">
              <h1 className={styles.title}>
                <Link className={styles.btnBack} to="/contacts">
                  <UI.Icon className={styles.iconBack} icon="arrow-right" />
                </Link>
                {formKey === `create` && `Создать новый контакт`}
                {formKey !== `create` && `Контакт (ID: ${formKey})`}
              </h1>
            </Grid.Col>
          </Grid.Row>
          <Grid.Row>
            <Grid.Col xs="20">
              <Media className={styles.media}
                left={<Image photo={formKey !== `create` && this.props.state.contacts[formKey].data.details.photo} fields={fields} />}
                body={<Description {...this.props} />}
              />
            </Grid.Col>
          </Grid.Row>
        </Grid.Container>
        {formKey === `create` && <Button className={cn(styles.btnSave, { [styles.btnHidden]: pristine })} kind="success" size="md" block>Добавить</Button>}
        {formKey !== `create` && <Button className={cn(styles.btnSave, { [styles.btnHidden]: pristine })} kind="warning" size="md" block>Сохранить</Button>}
      </Form.Container>
    );
  }
}

const formSettings = {
  form: `contact`,
  fields: formFields,
  validate,
};

export default reduxForm(formSettings)(Header);
