#!/usr/bin/env python3
#coding: utf-8

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

"""
Exports the image histogram to a text file,
so that it can be used by other programs
and loaded into spreadsheets.

The resulting file is a CSV file (Comma Separated
Values), which can be imported
directly in most spreadsheet programs.

The first two columns are the bucket boundaries,
followed by the selected columns. The histogram
refers to the selected image area, and
can use either Sample Average data or data
from the current drawable only.;

The output is in "weighted pixels" - meaning
all fully transparent pixels are not counted.

Check the pika-histogram call
"""

import csv
import math
import sys

import gi
gi.require_version('Pika', '3.0')
from gi.repository import Pika
gi.require_version('PikaUi', '3.0')
from gi.repository import PikaUi
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gio
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

def N_(message): return message
def _(message): return GLib.dgettext(None, message)

class StringEnum:
    """
    Helper class for when you want to use strings as keys of an enum. The values would be
    user facing strings that might undergo translation.

    The constructor accepts an even amount of arguments. Each pair of arguments
    is a key/value pair.
    """
    def __init__(self, *args):
        self.keys = []
        self.values = []

        for i in range(len(args)//2):
            self.keys.append(args[i*2])
            self.values.append(args[i*2+1])

    def get_tree_model(self):
        """ Get a tree model that can be used in GTK widgets. """
        tree_model = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_STRING)
        for i in range(len(self.keys)):
            tree_model.append([self.keys[i], self.values[i]])
        return tree_model

    def __getattr__(self, name):
        """ Implements access to the key. For example, if you provided a key "red", then you could access it by
            referring to
               my_enum.red
            It may seem silly as "my_enum.red" is longer to write then just "red",
            but this provides verification that the key is indeed inside enum. """
        key = name.replace("_", " ")
        if key in self.keys:
            return key
        raise AttributeError("No such key string " + key)


output_format_enum = StringEnum(
    "pixel count", _("Pixel count"),
    "normalized", _("Normalized"),
    "percent", _("Percent")
)


def histogram_export(procedure, img, layers, gio_file,
                     bucket_size, sample_average, output_format):
    layers = img.list_selected_layers()
    layer = layers[0]
    if sample_average:
        new_img = img.duplicate()
        layer = new_img.merge_visible_layers(Pika.MergeType.CLIP_TO_IMAGE)

    channels_txt = ["Value"]
    channels_pika = [Pika.HistogramChannel.VALUE]
    if layer.is_rgb():
        channels_txt += ["Red", "Green", "Blue", "Luminance"]
        channels_pika += [Pika.HistogramChannel.RED, Pika.HistogramChannel.GREEN, Pika.HistogramChannel.BLUE,
                          Pika.HistogramChannel.LUMINANCE]
    if layer.has_alpha():
        channels_txt += ["Alpha"]
        channels_pika += [Pika.HistogramChannel.ALPHA]

    try:
        with open(gio_file.get_path(), "wt") as hfile:
            writer = csv.writer(hfile)
            histo_proc = Pika.get_pdb().lookup_procedure('pika-drawable-histogram')
            histo_config = histo_proc.create_config()
            histo_config.set_property('drawable', layer)

            # Write headers:
            writer.writerow(["Range start"] + channels_txt)

            max_index = 1.0/bucket_size if bucket_size > 0 else 1
            i = 0
            progress_bar_int_percent = 0
            while True:
                start_range = i * bucket_size
                i += 1
                if start_range >= 1.0:
                    break

                histo_config.set_property('start-range', float(start_range))
                histo_config.set_property('end-range', float(min(start_range + bucket_size, 1.0)))
                row = [start_range]
                for channel in channels_pika:
                    histo_config.set_property('channel', channel)
                    result = histo_proc.run(histo_config)

                    if output_format == output_format_enum.pixel_count:
                        count = int(result.index(5))
                    else:
                        pixels = result.index(4)
                        count = (result.index(5) / pixels) if pixels else 0
                        if output_format == output_format_enum.percent:
                            count = "%.2f%%" % (count * 100)
                    row.append(str(count))
                writer.writerow(row)

                # Update progress bar
                fraction = i / max_index
                # Only update the progress bar if it changed at least 1% .
                new_percent = math.floor(fraction * 100)
                if new_percent != progress_bar_int_percent:
                    progress_bar_int_percent = new_percent
                Pika.progress_update(fraction)
    except IsADirectoryError:
        return procedure.new_return_values(Pika.PDBStatusType.EXECUTION_ERROR,
                                           GLib.Error(_("File is either a directory or file name is empty.")))
    except FileNotFoundError:
        return procedure.new_return_values(Pika.PDBStatusType.EXECUTION_ERROR,
                                           GLib.Error(_("Directory not found.")))
    except PermissionError:
        return procedure.new_return_values(Pika.PDBStatusType.EXECUTION_ERROR,
                                           GLib.Error("You do not have permissions to write that file."))

    if sample_average:
        new_img.delete()

    return procedure.new_return_values(Pika.PDBStatusType.SUCCESS, GLib.Error())


def run(procedure, run_mode, image, n_layers, layers, config, data):
    if run_mode == Pika.RunMode.INTERACTIVE:
        PikaUi.init("histogram-export.py")

        dialog = PikaUi.ProcedureDialog.new(procedure, config, _("Histogram Export..."))
        dialog.fill(None)

        if not dialog.run():
            return procedure.new_return_values(Pika.PDBStatusType.CANCEL,
                                               GLib.Error())

    gio_file       = config.get_property('file')
    bucket_size    = config.get_property("bucket-size")
    sample_average = config.get_property("sample-average")
    output_format  = config.get_property("output-format")

    if gio_file is None:
        error = 'No file given'
        return procedure.new_return_values(Pika.PDBStatusType.CALLING_ERROR,
                                           GLib.Error(error))

    result = histogram_export(procedure, image, layers, gio_file,
                              bucket_size, sample_average, output_format)

    return result


class HistogramExport(Pika.PlugIn):

    ## Parameters ##
    __gproperties__ = {
        # TODO: GFile props still don't have labels + only load existing files
        # (here we likely want to create a new file).
        "file": (Gio.File,
                 _("Histogram _File"),
                 "Histogram export file",
                 GObject.ParamFlags.READWRITE),
        "bucket-size":  (float,
                         _("_Bucket Size"),
                         "Bucket Size",
                         0.001, 1.0, 0.01,
                         GObject.ParamFlags.READWRITE),
        "sample-average": (bool,
                           _("Sample _Average"),
                           "Sample Average",
                           False,
                           GObject.ParamFlags.READWRITE),
        "output-format": (str,
                          _("Output _format"),
                          "Output format: 'pixel count', 'normalized', 'percent'",
                          "pixel count",
                          GObject.ParamFlags.READWRITE),
    }

    ## PikaPlugIn virtual methods ##
    def do_set_i18n(self, procname):
        return True, 'pika30-python', None

    def do_query_procedures(self):
        return ['histogram-export']

    def do_create_procedure(self, name):
        procedure = None
        if name == 'histogram-export':
            procedure = Pika.ImageProcedure.new(self, name,
                                                Pika.PDBProcType.PLUGIN,
                                                run, None)

            procedure.set_image_types("*")
            procedure.set_documentation (
                _("Exports the image histogram to a text file (CSV)"),
                globals()["__doc__"],  # This includes the docstring, on the top of the file
                name)
            procedure.set_menu_label(_("_Export histogram..."))
            procedure.set_attribution("João S. O. Bueno",
                                      "(c) GPL V3.0 or later",
                                      "2014")
            procedure.add_menu_path("<Image>/Colors/Info/")

            procedure.add_argument_from_property(self, "file")
            procedure.add_argument_from_property(self, "bucket-size")
            procedure.add_argument_from_property(self, "sample-average")
            procedure.add_argument_from_property(self, "output-format")

        return procedure


Pika.main(HistogramExport.__gtype__, sys.argv)
