import { ref, Ref } from "vue";
import { actionSheetController } from "@ionic/vue";
import { reorderThreeOutline, trashOutline } from "ionicons/icons";
import { PhotoDTO } from "@studio-m/app-types/src/shooting.types";

interface UsePhotoActionsOptions {
  onReorderStart: () => void;
  onDeleteStart: () => void;
}

export function usePhotoActions({
  onReorderStart,
  onDeleteStart,
}: UsePhotoActionsOptions) {
  const isDeleteMode = ref(false);
  const selectedPhotos = ref<PhotoDTO[]>([]);

  const openActionMenu = async () => {
    const actionSheet = await actionSheetController.create({
      header: "Actions",
      buttons: [
        {
          text: "Réorganiser les photos",
          icon: reorderThreeOutline,
          handler: () => {
            onReorderStart();
          },
        },
        {
          text: "Supprimer des photos",
          icon: trashOutline,
          handler: () => {
            isDeleteMode.value = true;
            onDeleteStart();
          },
        },
        {
          text: "Annuler",
          role: "cancel",
        },
      ],
    });

    await actionSheet.present();
  };

  const handlePhotoClick = (photo: PhotoDTO) => {
    if (isDeleteMode.value) {
      const index = selectedPhotos.value.findIndex((p) => p.id === photo.id);
      if (index === -1) {
        selectedPhotos.value.push(photo);
      } else {
        selectedPhotos.value.splice(index, 1);
      }
    }
  };

  const exitDeleteMode = () => {
    selectedPhotos.value = [];
    isDeleteMode.value = false;
  };

  const isPhotoSelected = (photoId: string) => {
    return selectedPhotos.value.some((photo) => photo.id === photoId);
  };

  return {
    isDeleteMode,
    selectedPhotos,
    openActionMenu,
    handlePhotoClick,
    exitDeleteMode,
    isPhotoSelected,
  };
}
