#!/usr/bin/env python
# -*- coding: utf-8 -*-

## @file config_reader.py
## @brief Reads and provides configuration values for the alpha-blending system.
## @details
## This module loads a JSON configuration file (`config.json`) and exposes
## accessor methods that return typed values for physical projector parameters,
## image metadata, blend settings, and runtime options.

import json
import os


class ConfigReader(object):
    """
    @brief A helper class for reading configuration values from config.json.
    @details
    The class loads parameters only once during initialization and provides
    getters which will be used by the MainAlphaBlender class.
    @see main_alpha_blender.py
    """

    CONFIG_FILENAME = "config.json"

    def __init__(self):
        """
        @brief This is the constructor and this loads config.json into memory if it exists.
        @details
        If the JSON file does not exist, default values will be used.
        """
        self.__config = {}
        self._load_config()

    def _load_config(self):
        """
        @brief Internal function to load JSON configuration.
        @details
        This function opens config.json (if present) and loads settings into a dictionary.
        For the missing files, this will not raise any error but defaults will be used instead.
        """
        if not os.path.exists(self.CONFIG_FILENAME):
            print(f"Warning: {self.CONFIG_FILENAME} not found. Using defaults.")
            return

        with open(self.CONFIG_FILENAME, 'r') as f:
            self.__config = json.load(f)

    def getProjectedImageWidth(self):
        """
        @brief This returns the physical width of the projected image.
        @details This is the width of *one* projector's image in centimeters.
        @return return  Projected image width in float or 0 if not found or invalid.
        @see main_alpha_blender.py
        """
        return float(self.__config.get("projected_image_width", 0))

    def getDistanceBetweenProjectors(self):
        """
        @brief This returns the physical distance between projector lenses in centimeters.
        @details this is used to compute percentage of overlap between the two images.
        @return return  Distance between projectors in float or 0 in case of wrong format.
        @see main_alpha_blender.py
        """
        return float(self.__config.get("distance_between_projectors", 0))

    def getImageWidth(self):
        """
        @brief this returns the pixel width of the input image.
        @return return the Pixel width (default 1920) in int.
        @see main_alpha_blender.py
        """
        return int(self.__config.get("image_width", 1920))

    def getImageHeight(self):
        """
        @brief this returns the pixel height of the input image.
        @return return the Pixel height (default 1080) in int.
        @see main_alpha_blender.py
        """
        return int(self.__config.get("image_height", 1080))

    def getImageName(self):
        """
        @brief this returns the file name of the image to process.
        @return return Input image path in string.
        @see main_alpha_blender.py
        """
        return self.__config.get("image_name", "")

    def getSide(self):
        """
        @brief This returns which projector side this instance represents.
        @details Valid values are `"left"` or `"right"`. Defaults to `"left"`.
        @return return the  Blend side in lowercase in string i.e "left" or "right".
        @see main_alpha_blender.py
        """
        return self.__config.get("side", "left").lower()

    def getGamma(self):
        """
        @brief This returns the gamma value used for mask correction.
        @details Typical projector gamma is around 2.2.
        @return return Gamma value in float.
        @see main_alpha_blender.py
        """
        return float(self.__config.get("gamma", 2.2))