import { memo, useMemo } from '../../../lib/teact/teact';
import { getActions } from '../../../global';

import type { TabState } from '../../../global/types';

import { prepareMapUrl } from '../../../util/map';

import useLang from '../../../hooks/useLang';
import useLastCallback from '../../../hooks/useLastCallback';

import Button from '../../ui/Button';
import Modal from '../../ui/Modal';

import styles from './MapModal.module.scss';

export type OwnProps = {
  modal: TabState['mapModal'];
};

const OpenMapModal = ({ modal }: OwnProps) => {
  const { closeMapModal } = getActions();

  const { point: geoPoint, zoom } = modal || {};

  const lang = useLang();

  const isOpen = Boolean(geoPoint);

  const handleClose = useLastCallback(() => {
    closeMapModal();
  });

  const [googleUrl, bingUrl, appleUrl, osmUrl] = useMemo(() => {
    if (!geoPoint) {
      return [];
    }

    const google = prepareMapUrl('google', geoPoint, zoom);
    const bing = prepareMapUrl('bing', geoPoint, zoom);
    const apple = prepareMapUrl('apple', geoPoint, zoom);
    const osm = prepareMapUrl('osm', geoPoint, zoom);

    return [google, bing, apple, osm];
  }, [geoPoint, zoom]);

  const openUrl = useLastCallback((url: string) => {
    closeMapModal();
    window.open(url, '_blank', 'noopener');
  });

  const handleGoogleClick = useLastCallback(() => {
    openUrl(googleUrl!);
  });

  const handleBingClick = useLastCallback(() => {
    openUrl(bingUrl!);
  });

  const handleAppleClick = useLastCallback(() => {
    openUrl(appleUrl!);
  });

  const handleOsmClick = useLastCallback(() => {
    openUrl(osmUrl!);
  });

  return (
    <Modal
      contentClassName={styles.root}
      title={lang('OpenMapWith')}
      isOpen={isOpen}
      onClose={handleClose}
      isSlim
    >
      <div className={styles.buttons}>
        <Button noForcedUpperCase fluid size="smaller" onClick={handleGoogleClick}>
          Google Maps
        </Button>
        <Button noForcedUpperCase fluid size="smaller" onClick={handleAppleClick}>
          Apple Maps
        </Button>
        <Button noForcedUpperCase fluid size="smaller" onClick={handleBingClick}>
          Bing Maps
        </Button>
        <Button noForcedUpperCase fluid size="smaller" onClick={handleOsmClick}>
          OpenStreetMap
        </Button>
      </div>
      <div className="dialog-buttons mt-2">
        <Button className="confirm-dialog-button" isText onClick={handleClose}>
          {lang('Cancel')}
        </Button>
      </div>
    </Modal>
  );
};

export default memo(OpenMapModal);
