import { Input, Label } from '@bjsdistribution/dms-component-library';
import React from 'react';
import styled from 'styled-components';
import { errorAtom, useAtom, useOrderDetails } from '../../../../store';
import { handleKeyDown } from '../../../../utils';

// const LANDLINE_REGEX = /^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$/;

function ContactHome() {
  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 onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const val = e.target.value;
    const updatedOrderRecipient = {
      ...(orderRecipient === null
        ? { contact_home: val }
        : { ...orderRecipient, contact_home: val }),
    };

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

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

  return (
    <MainContainer>
      <Wrapper>
        <Label label="Contact Home" htmlFor="contact-home" />
        {/* {showCopyIcon && <CopyText text={orderRecipient?.contact_home!} />} */}
      </Wrapper>
      <InputContainer>
        <CustomInput
          name="contact-home"
          type="text"
          placeholder="Home Number"
          onKeyDown={handleKeyDown}
          minLength={10}
          maxLength={10}
          value={orderRecipient?.contact_home || ''}
          onChange={onChange}
        />

        {/* <CallCustomer /> */}
      </InputContainer>
      {errors.homeContactError && (
        <ErrorText>{errors.homeContactError}</ErrorText>
      )}
    </MainContainer>
  );
}

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

const CustomInput = styled(Input)`
  width: 80%;
  // @media (max-width: 1024px) {
  //   width: 80%;
  // }

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

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

const MainContainer = 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 ContactHome;
