import React, { useEffect, useState } from 'react';
import { CardButton, ContainerButton, ContainerImage, ContainerText, TextImage } from './styles';
import { AtomButtonDefault } from 'native/atoms/Buttons/Default';
import { AtomBody } from 'native/atoms/Typography/Body';
import { theme } from 'data/dataMx';
import { AtomHeadline } from 'native/atoms/Typography/Headline';
import { AtomCaption } from 'native/atoms/Typography/Caption';
import { AtomImage } from 'native/atoms/Image';
import { ButtoniconTypeDesign, ButtonSize, ButtonTypeDesign, IconAlign } from 'native/atoms/Buttons/ButtonProps';
import { AtomButtonOutline } from 'native/atoms/Buttons/Outline';
import { AtomButtonGhost } from 'native/atoms/Buttons/Ghost';
import { AtomButtonIcon } from 'native/atoms/Buttons/Icon';

export type OrientationCard = 'row' | 'column';

interface IItemProps {
  /** Disabled button */
  disabled: boolean;
  /** Click action */
  onClick?: (e?: any) => void;
  /** text in button */
  text: string;
  /** Type Button (ghost | default | icon | outline) */
  type: ButtonTypeDesign;
  /** icon Name */
  icon?: string;
  /** position of the icon relative to the text */
  iconAlign?: IconAlign;
  /** If Button is icon */
  typeButtonIcon?: ButtoniconTypeDesign;
  /** Size Button */
  size: ButtonSize
}
export interface ISizeProps {
  width: string;
  height: string;
  heightButton: ButtonSize;
};

export interface ICardButtonProps {
  /** Click action */
  handleClick: () => void;
  /** Principal text */
  title: string;
  /** Description text */
  description: string;
  /** text in button */
  textButton: string;
  /** Image location */
  src: string;
  /** Can be: column or row */
  orientation?: OrientationCard;
  /** subtitle text */
  category?: string;
  /** Card height */
  heightCard?: string;
  /** Click width */
  widthCard?: string;
  /** Disabled button */
  disabled?: boolean;
  /** Text tag, if don't have it, don't show it */
  tagText?: string;
  /** Type Button (ghost | default | icon | outline) */
  typeButton?: ButtonTypeDesign
  /** If Button is icon */
  iconButton?: string,
  /** If Button is icon */
  typeButtonIcon?: ButtoniconTypeDesign
};

const Button = ({
  disabled,
  onClick,
  text,
  icon,
  iconAlign,
  typeButtonIcon,
  type,
  size
}: IItemProps) => {
  switch (type) {
    case 'outline':
      return <AtomButtonOutline
        disabled={disabled}
        onPress={onClick}
        color={'primary'}
        text={text}
        width={'100%'}
        size={size}
      />;
    case 'default':
      return <AtomButtonDefault
        disabled={disabled}
        onPress={onClick}
        color={'primary'}
        text={text}
        width={'100%'}
        size={size}
      />
    case 'ghost':
      return <AtomButtonGhost
        disabled={disabled}
        onPress={onClick}
        color={'primary'}
        text={text}
        width={'100%'}
        size={size}
      />
    case 'icon':
      return <AtomButtonIcon
        disabled={disabled}
        onPress={onClick}
        color={'primary'}
        text={text}
        icon={icon ?? ''}
        iconAlign={iconAlign}
        width={'100%'}
        typeButtonIcon={typeButtonIcon}
        size={size}
      />
  }
};

export const OrganismsCardButton = ({
  handleClick,
  disabled = false,
  heightCard,
  widthCard,
  title,
  description,
  category,
  textButton,
  tagText,
  src,
  orientation = 'column',
  typeButton = 'default',
  iconButton = 'arrow_forward_ios',
  typeButtonIcon
}: ICardButtonProps) => {
  const [size, setSize] = useState<ISizeProps>({
    width: widthCard ? widthCard : orientation == 'column' ? '239px' : '320px',
    height: heightCard ? heightCard : orientation == 'column' ? '342px' : '168px',
    heightButton: 'small'
  })

  const [style, setStyle] = useState(orientation == 'column' ? {
    'borderTopLeftRadius': '8px',
    'borderTopRightRadius': '8px',
  } : {
    'borderTopLeftRadius': '8px',
    'borderBottomLeftRadius': '8px',
  });

  const assignHeightButton = (widthButton: number) => {
    if (orientation == 'column') {
      return (widthButton < 180) ? 'small' : ((widthButton < 280) ? 'medium' : 'large');
    } else {
      return (widthButton < 340) ? 'small' : ((widthButton < 440) ? 'medium' : 'large');
    }
  }

  useEffect(() => {
    let width = widthCard ? widthCard : orientation == 'column' ? '239px' : '320px';
    let widthButton = parseInt(width);
    let heightButton: ButtonSize = assignHeightButton(widthButton);
    setSize({
      width,
      height: heightCard ? heightCard : orientation == 'column' ? '342px' : '168px',
      heightButton
    })
  }, [widthCard, heightCard, orientation]);

  useEffect(() => {
    setStyle(orientation == 'column' ? {
      'borderTopLeftRadius': '8px',
      'borderTopRightRadius': '8px',
    } : {
      'borderTopLeftRadius': '8px',
      'borderBottomLeftRadius': '8px',
    })
  }, [orientation]);

  return <>
    <CardButton theme={theme} width={size.width}
      height={size.height} orientation={orientation}>
      {tagText && <TextImage theme={theme} orientation={orientation} key={title}>
        <AtomCaption size='small' text={tagText} align='center' color={'neutral100'} weight={'extralight'}></AtomCaption>
      </TextImage>}
      <ContainerImage orientation={orientation} theme={theme}>
        <AtomImage src={src} alt={title} imgHeight={'100%'} imgWidth={'100%'} styles={style} resizeMode='cover'></AtomImage>
      </ContainerImage>
      <ContainerText orientation={orientation} theme={theme}>
        {category && <AtomBody size='small' text={category} styles={{ 'textAlign': orientation == 'column' ? 'center' : 'left' }} color={'neutral500'} weight={'light'} />}
        <AtomHeadline size='small' text={title} color={'neutral700'} weight={'bold'} styles={{ 'textAlign': orientation == 'column' ? 'center' : 'left', margin: '6px 0' }}></AtomHeadline>
        {orientation == 'column' && <AtomBody size='medium' text={description} styles={orientation == 'column' ? { 'textAlign': 'center' }: {}} color={'neutral600'} weight={'light'} />}
        <ContainerButton>
          <Button text={textButton} type={typeButton} onClick={handleClick} disabled={disabled} size={size.heightButton} icon={iconButton} typeButtonIcon={typeButtonIcon} />
        </ContainerButton>
      </ContainerText>
    </CardButton>
  </>
};
