import { connect } from 'react-redux';
import React from 'react';
import styled from 'styled-components';
import {
  IconButton,
  PrimaryIconButton,
} from '../../../components/common/buttons/IconButton';
import {
  addNewLogic,
  deleteLogic,
  updateLogicProperty,
} from '../../../redux/action-creators/form-builder-actions/form-builder.actions';

const FormLogic = ({
  elementDetails,
  addNewLogic,
  updateLogicProperty,
  deleteLogic,
}) => {
  return (
    <div>
      <div className="py-2 border-top">
        {elementDetails.logics.length > 0 ? (
          <div>
            {elementDetails.logics.map((logic, i) => {
              return (
                <div key={i}>
                  <LogicWrapper className="logic-wrapper">
                    <div className="if-logic-wrapper">
                      <div className="d-flx-alc if-logic-title">
                        <div className="mr-2">
                          <select
                            value={logic.condition}
                            onChange={e =>
                              updateLogicProperty(
                                i,
                                e.target.value,
                                'condition'
                              )
                            }
                            style={{
                              padding: '5px',
                              color: '#6b7184',
                              background: '#f1f1f5',
                              height: '44px',
                              width: '120px',
                              border: 'none',
                            }}
                          >
                            <option value="">condition</option>
                            <option value="equals">Equals</option>
                          </select>
                        </div>
                        {/* <p>of the following conditions are met</p> */}
                      </div>
                      <LogicChoice className="logic-choice pb-2">
                        <div className="">
                          <div className="mr-2">
                            <select
                              value={logic.value}
                              onChange={e =>
                                updateLogicProperty(i, e.target.value, 'value')
                              }
                              style={{
                                padding: '5px',
                                color: '#6b7184',
                                background: '#f1f1f5',
                                height: '44px',
                                width: '250px',
                                border: 'none',
                              }}
                            >
                              <option value="">Select option</option>
                              {elementDetails.options.map((opt, i) => {
                                return (
                                  <option key={i} value={opt?.label}>
                                    {opt?.label}
                                  </option>
                                );
                              })}
                            </select>
                          </div>
                        </div>
                        <div className="">
                          <select
                            value={logic.action}
                            onChange={e =>
                              updateLogicProperty(i, e.target.value, 'action')
                            }
                            style={{
                              padding: '5px',
                              color: '#6b7184',
                              background: '#f1f1f5',
                              height: '44px',
                              width: '100px',
                              border: 'none',
                            }}
                          >
                            <option value="">select action</option>
                            <option value="show">Show</option>
                            <option value="hide">Hide</option>
                          </select>
                        </div>
                        <div className="">
                          <select
                            value={logic.targets[0] ? logic.targets[0] : ''}
                            onChange={e =>
                              updateLogicProperty(i, e.target.value, 'targets')
                            }
                            style={{
                              padding: '5px',
                              color: '#6b7184',
                              background: '#f1f1f5',
                              height: '44px',
                              width: '300px',
                              border: 'none',
                            }}
                          >
                            <option value="">select target</option>
                          </select>
                        </div>
                        <div className="d-flx-alc-jsb">
                          {/* <IconButton style={{marginLeft: "10px"}} svg="add-box" className="add-btn" /> */}
                          <span onClick={() => deleteLogic()}>
                            {' '}
                            <IconButton svg="close" className="delete-btn" />
                          </span>
                        </div>
                      </LogicChoice>
                    </div>
                  </LogicWrapper>
                </div>
              );
            })}
          </div>
        ) : (
          <div className="d-flx-alc">
            <span onClick={() => addNewLogic()}>
              <PrimaryIconButton
                svg="add-box"
                title="Add Logic to this question."
                color="#838CA2"
              />
            </span>
            <a href="/">Learn more about Logic </a>
          </div>
        )}
      </div>
    </div>
  );
};

export default connect(null, { addNewLogic, updateLogicProperty, deleteLogic })(
  FormLogic
);

const LogicChoice = styled.div`
  position: relative;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: space-between;
  padding-top: 2rem;

  > * {
    &:first-child,
    &:nth-child(3n) {
      width: 32%;
    }
    &:nth-child(2n) {
      width: 16%;
    }
    &:last-child {
      width: 12%;
    }
  }
  .delete-btn {
    color: #d50033;
    svg {
      width: 20px;
      height: 20px;
    }
  }
  .add-btn {
    color: #61677c;
    svg {
      width: 20px;
      height: 20px;
    }
  }
`;

const LogicWrapper = styled.div`
  padding-left: 100px;
  position: relative;
  .if-logic-wrapper {
    padding-bottom: 5rem;
    position: relative;
    &:before {
      content: 'IF';
      width: 55px;
      height: 35px;
      background: #e8edff;
      border-radius: 2px;
      position: absolute;
      left: -100px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-family: var(--ff-medium);
      color: #0a1045;
      font-size: 1.4rem;
    }
    &:after {
      content: '';
      width: 1px;
      height: 100%;
      background: #e8edff;
      border-radius: 2px;
      position: absolute;
      left: -70px;
      top: 0;
    }
    .if-logic-title {
      > * {
        &:first-child {
          width: 81px;
        }
      }
    }
  }
  .then-logic-wrapper {
    position: relative;
    &:before {
      content: 'THEN';
      width: 55px;
      height: 35px;
      background: #e8edff;
      border-radius: 2px;
      position: absolute;
      left: -100px;
      top: -10px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-family: var(--ff-medium);
      color: #0a1045;
      font-size: 1.4rem;
    }
  }
`;
