import { Input, Label } from '@bjsdistribution/dms-component-library';
import React from 'react';
import styled from 'styled-components';
import CopyText from '../../../../../../common/copy/CopyText';
import CallCustomer from './CallCustomer';
import { errorAtom, useAtom, useOrderDetails } from '../../../../store';
import { handleKeyDown } from '../../../../utils';
// const MOBILE_REGEX = /^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/;

function ContactMobile() {
  const [errors, setErrors] = useAtom(errorAtom);

  // Show call icons only if number is valid
  // show copy icon if there is input value
  const [orderDetails, setOrderDetails] = useOrderDetails();
  const orderRecipient = orderDetails?.order_recipient;

  const showCopyIcon =
    orderRecipient?.contact_mobile !== undefined &&
    orderRecipient?.contact_mobile !== null &&
    orderRecipient?.contact_mobile?.length > 0;

  const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const val = e.target.value;
    const updatedOrderRecipient = {
      ...(orderRecipient === null
        ? { contact_mobile: val }
        : { ...orderRecipient, contact_mobile: val }),
    };

    setOrderDetails(prev => ({
      ...prev,
      order_recipient: updatedOrderRecipient,
    }));

    if (val) {
      if (val.length < 10) {
        setErrors(prevErrors => ({
          ...prevErrors,
          mobileError: 'Phone number must be at least 10 digits.',
        }));
      } else if (val.length > 11) {
        setErrors(prevErrors => ({
          ...prevErrors,
          mobileError: 'Phone number must not exceed 11 digits.',
        }));
      } else {
        setErrors(prevErrors => ({
          ...prevErrors,
          mobileError: '',
        }));
      }
    } else {
      setErrors(prevErrors => ({
        ...prevErrors,
        mobileError: '',
      }));
    }
  };

  return (
    <Container>
      <Wrapper>
        <Label label="Contact Mobile" htmlFor="contact-work" />
        {showCopyIcon && <CopyText text={orderRecipient?.contact_mobile!} />}
      </Wrapper>
      <InputContainer>
        <CustomInput
          name="contact-work"
          minLength={10}
          maxLength={10}
          type="text"
          placeholder="Mobile Number"
          onKeyDown={handleKeyDown}
          value={orderRecipient?.contact_mobile || ''}
          onChange={onChange}
        />
        <CallCustomer />
      </InputContainer>
      {errors.mobileError && <ErrorText>{errors.mobileError}</ErrorText>}
    </Container>
  );
}

const InputContainer = styled.div`
  display: flex;
  gap: 7px;
  align-items: center;
`;

const Wrapper = styled.div`
  display: flex;
  align-items: center;
  gap: 5px;
`;

const CustomInput = styled(Input)`
  width: 80%;

  &::-webkit-outer-spin-button,
  &::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
  }
  &:focus-visible {
    outline: none;
  }
`;

const Container = styled.div`
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
`;

const ErrorText = styled.span`
  color: red;
  font-size: 0.85rem;
  margin-top: 4px;
`;

export default ContactMobile;
