import React, { useState } from 'react';
import { Button, MapView, ParcelInfoPanel } from '../../../../components/UiComponents';
import { usePekContext } from '../../../../contexts/PekContext';
import styles from './PekMapView.module.css';

import { useLanguage } from '../../../../providers/language/LanguageContext';

const PekMapView: React.FC = () => {
    const { t } = useLanguage();

  const [showWfsParcels, setShowWfsParcels] = useState(false);
  const {
    instanceId,
    mapCenter,
    mapZoomBounds,
    parcelGeometries,
    selectionSummary,
    handleMapClick,
    handleParcelClick,
    selectedParcels,
    removeParcel,
    clearSelectedParcels,
    isPanelOpen,
    setIsPanelOpen
  } = usePekContext();

  return (
    <>
      <div className={styles.pekMapWrapper}>
        <div className={styles.checkboxRow}>
          <label className={styles.checkboxLabel}>
            <input
              type="checkbox"
              checked={showWfsParcels}
              onChange={(e) => setShowWfsParcels(e.target.checked)}
            />
            <span>{t('Alle Parzellen anzeigen')}</span>
          </label>
          <Button
            variant="secondary"
            size="sm"
            onClick={clearSelectedParcels}
            disabled={selectedParcels.length === 0}
          >
            {t('Alle Parzellen abwählen')}
          </Button>
        </div>
        <div className={styles.mapContainer}>
          <MapView
            parcels={parcelGeometries}
            combinedOutline={selectionSummary?.combinedOutline}
            center={mapCenter || undefined}
            zoomBounds={mapZoomBounds || undefined}
            onMapClick={handleMapClick}
            onParcelClick={handleParcelClick}
            height="100%"
            emptyMessage={t('Klicken Sie auf die Karte')}
            showWfsParcels={showWfsParcels}
          />
        </div>
      </div>

      <ParcelInfoPanel
        isOpen={isPanelOpen}
        onClose={() => setIsPanelOpen(false)}
        parcels={selectedParcels}
        selectionSummary={selectionSummary}
        onRemoveParcel={removeParcel}
        instanceId={instanceId}
      />
    </>
  );
};

export default PekMapView;
