package id.co.gtc.erhacam;

import Config.SomeCodes;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.opencv_core.Mat;
import org.tinylog.Logger;


import java.io.File;
import java.nio.file.Path;

import static Config.SomeCodes.ShowAlert;
import static Config.SomeCodes.config;

public class PhotoRow {
    @FXML
    private Label datetime;
    @FXML
    private Label prefix;
    @FXML
    private HBox photos;

    private final String borderstyle = "-fx-border-color: black; -fx-border-width: 1px;";

    public void setDatetime(String datetime){
        this.datetime.setText(datetime);
        this.datetime.setStyle(borderstyle);
    }

    public void setPrefix(String prefix){
        this.prefix.setText(prefix);
        this.prefix.setStyle(borderstyle);
    }

    public void setPhotos(int width, int height, String... thumbnails){
        photos.setSpacing(10);
        for(String photopath : thumbnails){
            ImageView imgview = createImageView(loadImage(photopath), width, height);
            if (imgview!=null){
                photos.getChildren().add(imgview);
                //HBox.setMargin(imgview, new Insets(5, 5, 5, 5));
                imgview.setStyle(borderstyle);
                imgview.setOnMouseClicked(e->{
                    if (e.getClickCount()>=2){
                        //System.out.println("Photo path: "+photopath);
                        File ff = new File(photopath);
//                        System.out.println("Config exists : "+(config!=null));
//                        System.out.println("Photo directory: "+config.getPhotoDirectory());
//                        System.out.println("Full quality directory: "+config.getFullQualityDirectory());
//                        System.out.println("Full Quality Crop directory: "+config.getFullQualityCropDirectory());
//                        System.out.println("Reduced quality directory: "+config.getCompressedDirectory());
//                        System.out.println("Reduced quality crop directory: "+config.getCompressedCropDirectory());
                        //String hires = Path.of(config.getPhotoDirectory(), ff.getName()).toString();
                        String hires = Path.of(config.getFullQualityDirectory(), ff.getName()).toString();
                        //System.out.println("Hires: "+hires);
                        File hiresfile = new File(hires);
                        if (hiresfile.isFile()){
                            System.out.println("Opening file: "+hires);
                            SomeCodes.OpenPictureInDefaultViewer(hires);
                        } else ShowAlert(Alert.AlertType.ERROR, "Error", "File not found", "File not found: "+hires);
                        e.consume();
                    }
                });
            }
        }
    }

    private ImageView createImageView(Image img, int width, int height){
        if (img!=null){
            ImageView imgview = new ImageView(img);
            imgview.prefWidth(width);
            imgview.prefHeight(height);
            imgview.setFitHeight(height);
            imgview.setFitWidth(width);
            imgview.setPreserveRatio(true);
            return imgview;
        }
        return null;
    }

    private Image loadImage(String photopath){
        try{
            Mat mat = opencv_imgcodecs.imread(photopath);
            return SomeCodes.ConvertToImage(mat, 640,480);
        } catch (Exception e){
            Logger.error("Error loading image: " + photopath + ", Msg : " + e.getMessage());
        }
        return null;
    }
}
