import React, { ReactNode } from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import Modal from 'react-native-modal';

import { styles } from './styles';
import CloseIcon from '../../../assets/icons/close.svg';
import { Colors } from 'src/theme';

import { useNavigation } from '@react-navigation/native';
import { NAVIGATION_PAGES } from 'src/types';
import { ButtonVariants } from 'src/types/components';
import { Button } from '../Button';
import AddFriendSvg from '../../../assets/icons/user-plus.svg';
import AuthIcon from 'assets/icons/authenticate-user.svg';

export const WarningModal = ({
  isVisible,
  onClose,
  type,
  message,
  title,
  action,
  onModalHide,
  buttonTitle
}: {
  isVisible: boolean;
  onClose: () => void;
  type: string;
  message?: string | ReactNode;
  title?: string;
  action?: () => void;
  onModalHide?: () => void;
  buttonTitle?: string;
}) => {
  const navigation = useNavigation();

  const content = {
    offline: {
      message: 'Please check your Internet connection and try again.',
      buttons: [
        {
          text: 'OK',
          textColor: Colors.WHITE,
          color: Colors.DARK_BLUE,
          action: onClose,
          borderColor: Colors.DARK_BLUE
        }
      ]
    },
    unauthorized: {
      message:
        'You have to be a NomadMania member to use this feature. Log in or Set up a free account.',
      buttons: [
        {
          text: 'Login',
          textColor: Colors.DARK_BLUE,
          color: Colors.WHITE,
          action: () => {
            action && action();
            onClose();
            navigation.navigate(NAVIGATION_PAGES.LOGIN as never);
          },
          borderColor: Colors.DARK_BLUE
        },
        {
          text: 'Register',
          textColor: Colors.WHITE,
          color: Colors.DARK_BLUE,
          action: () => {
            action && action();
            onClose();
            navigation.navigate(NAVIGATION_PAGES.REGISTER as never);
          },
          borderColor: Colors.DARK_BLUE
        }
      ]
    },
    delete: {
      message,
      buttons: [
        {
          text: 'No',
          textColor: Colors.DARK_BLUE,
          color: Colors.WHITE,
          action: () => {
            onClose();
          },
          borderColor: Colors.DARK_BLUE
        },
        {
          text: buttonTitle ?? 'Delete',
          textColor: Colors.WHITE,
          color: Colors.RED,
          action: () => {
            onClose();
            action && action();
          },
          borderColor: Colors.RED
        }
      ]
    },
    success: {
      message,
      buttons: [
        {
          text: 'OK',
          textColor: Colors.WHITE,
          color: Colors.ORANGE,
          action: () => {
            onClose();
            action && action();
          },
          borderColor: Colors.ORANGE
        }
      ]
    },
    confirm: {
      message,
      buttons: [
        {
          text: 'No',
          textColor: Colors.DARK_BLUE,
          color: Colors.WHITE,
          action: () => {
            onClose();
          },
          borderColor: Colors.DARK_BLUE
        },
        {
          text: 'Yes',
          textColor: Colors.WHITE,
          color: Colors.DARK_BLUE,
          action: () => {
            onClose();
            action && action();
          },
          borderColor: Colors.DARK_BLUE
        }
      ]
    },
    friend: {
      message,
      buttons: [
        {
          text: 'No',
          textColor: Colors.DARK_BLUE,
          color: Colors.WHITE,
          action: () => {
            onClose();
          },
          borderColor: Colors.DARK_BLUE
        },
        {
          text: 'Yes',
          textColor: Colors.WHITE,
          color: Colors.DARK_BLUE,
          action: () => {
            onClose();
            action && action();
          },
          borderColor: Colors.DARK_BLUE
        }
      ]
    },
    authenticate: {
      message,
      buttons: [
        {
          text: 'No',
          textColor: Colors.DARK_BLUE,
          color: Colors.WHITE,
          action: () => {
            onClose();
          },
          borderColor: Colors.DARK_BLUE
        },
        {
          text: 'Yes',
          textColor: Colors.WHITE,
          color: Colors.DARK_BLUE,
          action: () => {
            onClose();
            action && action();
          },
          borderColor: Colors.DARK_BLUE
        }
      ]
    }
  };

  const modalContent = content[type as keyof typeof content] || {};

  return (
    <Modal isVisible={isVisible} onModalHide={onModalHide}>
      <View style={styles.centeredView}>
        <View style={styles.modalView}>
          <View style={{ alignSelf: 'flex-end' }}>
            <TouchableOpacity onPress={onClose}>
              <CloseIcon fill={Colors.LIGHT_GRAY} />
            </TouchableOpacity>
          </View>
          <View style={styles.modalContent}>
            {type === 'friend' ? (
              <View style={{ marginBottom: 16 }}>
                <AddFriendSvg fill={Colors.ORANGE} width={40} height={40} />
              </View>
            ) : type === 'authenticate' ? (
              <View style={{ marginBottom: 16 }}>
                <AuthIcon fill={Colors.ORANGE} width={40} height={40} />
              </View>
            ) : (
              <Text style={styles.modalTitle}>{title ?? 'Oops!'}</Text>
            )}
            <Text style={styles.modalText}>{modalContent.message}</Text>
            <View style={styles.buttonContainer}>
              {modalContent.buttons.map(
                (
                  button: {
                    text: string;
                    textColor: string;
                    color: string;
                    action: () => void;
                    borderColor: string;
                  },
                  idx: number
                ) => (
                  <Button
                    key={idx}
                    variant={ButtonVariants.OPACITY}
                    containerStyles={{
                      borderColor: button.borderColor,
                      backgroundColor: button.color,
                      width: type === 'offline' ? '60%' : '45%'
                    }}
                    textStyles={{
                      color: button.textColor
                    }}
                    onPress={button.action}
                    children={button.text}
                  />
                )
              )}
            </View>
          </View>
        </View>
      </View>
    </Modal>
  );
};
