# ===============================================================================
# Copyright 2016 Jake Ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================

# ============= enthought library imports =======================
from __future__ import absolute_import
from __future__ import print_function
import os

from PIL import Image
from affine import Affine
from pychron.core.geometry.affine import (
    AffineTransform,
    calculate_rigid_itransform_affine,
    calculate_rigid_itransform,
)

from chaco.array_plot_data import ArrayPlotData
from chaco.image_plot import ImagePlot
from chaco.plot import Plot
from chaco.tools.image_inspector_tool import ImageInspectorTool
from enable.base_tool import BaseTool
from enable.component_editor import ComponentEditor
from traits.api import (
    HasTraits,
    Str,
    Int,
    Bool,
    Any,
    Float,
    Property,
    on_trait_change,
    Instance,
    Event,
    Tuple,
    Enum,
)
from traitsui.api import View, UItem, Item, HGroup, VGroup

# ============= standard library imports ========================
from numpy import array

# ============= local library imports  ==========================
from traitsui.handler import Controller

from pychron.canvas.canvas2D.base_data_canvas import BaseDataCanvas
from pychron.core.geometry.affine import calculate_rigid_transform


class RegisteredImageInspectorTool(BaseTool):
    """A tool that captures the color and underlying values of an image plot."""

    # Indicates whether overlays listening to this tool should be visible.
    # visible = Bool(True)

    # Stores the value of self.visible when the mouse leaves the tool,
    # so that it can be restored when the mouse enters again.
    # _old_visible = Enum(None, True, False)  # Trait(None, Bool(True))

    goto_event = Event
    move_event = Event

    # def normal_key_pressed(self, event):
    #     if self.inspector_key.match(event):
    #         self.visible = not self.visible
    #         event.handled = True

    # def normal_mouse_leave(self, event):
    #     if self._old_visible is None:
    #         self._old_visible = self.visible
    #         self.visible = False
    #
    # def normal_mouse_enter(self, event):
    #     if self._old_visible is not None:
    #         self.visible = self._old_visible
    #         self._old_visible = None

    def normal_left_down(self, event):
        if event.shift_down:
            self.goto_event = event.x, event.y

    def normal_mouse_move(self, event):
        self.move_event = event.x, event.y