package com.hpay.hpay_mobile_api.services;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.math.BigDecimal;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import com.hpay.hpay_mobile_api.entities.ParrainCode;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.nio.file.Paths;

@Service
public class PhotoService {

    @Value("${client.photo.upload-dir}")
    private String uploadDir; // The directory to store photos

    @Value("${client.photo.profile.upload-dir}")
    private String clientPhotoUploadDir; // The directory to store photos

    @Value("${client.qr.parrainage.upload-dir}")
    private String clientQrParrainageUploadDir; // The directory to store photos


    @Value("${client.photo.base-url}")
    private String photoBaseUrl; // The directory to store photos

    @Value("${client.photo.profile.base-url}")
    private String clientPhotoBaseUrl; // The directory to store photos

    public String uploadPhoto(MultipartFile photo) throws IOException {

        // Create the directory if it doesn't exist
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            boolean mkdirs = dir.mkdirs();
        }

        // Get the original file name
        String fileName = UUID.randomUUID().toString() + photo.getOriginalFilename();

        // Define the path where the photo will be stored
        Path photoPath = Paths.get(uploadDir, fileName);

        // Save the photo to the file system
        Files.write(photoPath, photo.getBytes());

        return fileName;

    }

    public String uploadClientPhoto(MultipartFile photo) throws IOException {

        System.out.println("iciiiiii");
        // Create the directory if it doesn't exist
        File dir = new File(clientPhotoUploadDir);
        if (!dir.exists()) {
            boolean mkdirs = dir.mkdirs();
        }

        // Get the original file name
        String fileName = UUID.randomUUID().toString() + photo.getOriginalFilename();

        // Define the path where the photo will be stored
        Path photoPath = Paths.get(clientPhotoUploadDir, fileName);

        // Save the photo to the file system
        Files.write(photoPath, photo.getBytes());

        return fileName;

    }



    public String uploadQrCode(BufferedImage qrImage, String codeName) throws IOException {

        // Create the directory if it doesn't exist
        File dir = new File(clientQrParrainageUploadDir);

        if (!dir.exists()) {
            boolean mkdirs = dir.mkdirs();
        }


        String fileName = "qrcode_"+codeName+".png";
        File outputFile = new File(dir, fileName);

        ImageIO.write(qrImage, "png", outputFile);

        return  fileName;

    }



    public BufferedImage generateQRCodeImage(String text) throws WriterException {

        // QR code configuration
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.MARGIN, 1);

        // Generate QR code matrix
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, 200, 200, hints);

        // Create an image from the BitMatrix
        BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, 200, 200);
        graphics.setColor(Color.BLACK);


        for (int i = 0; i < 200; i++) {
            for (int j = 0; j < 200; j++) {
                if (bitMatrix.get(i, j)) {
                    image.setRGB(i, j, Color.BLACK.getRGB());
                }
            }
        }

        return image;
    }



}
