"""
/***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
"""

from qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtGui import QColor
from qgis.core import QgsCoordinateTransform, QgsPointXY, QgsProject, QgsSettings
from qgis.gui import QgsMapToolEmitPoint, QgsVertexMarker
from .util import epsg4326


class CaptureCoordinate(QgsMapToolEmitPoint):
    """Class to interact with the map canvas to capture the coordinate
    when the mouse button is pressed."""

    capturePoint = pyqtSignal(QgsPointXY)
    captureStopped = pyqtSignal()

    def __init__(self, canvas):
        QgsMapToolEmitPoint.__init__(self, canvas)
        self.canvas = canvas
        self.vertex = None

    def activate(self):
        """When activated set the cursor to a crosshair."""
        self.canvas.setCursor(Qt.CursorShape.CrossCursor)
        self.snapcolor = QgsSettings().value(
            "/qgis/digitizing/snap_color", QColor(Qt.GlobalColor.magenta)
        )

    def deactivate(self):
        self.removeVertexMarker()
        self.captureStopped.emit()

    def snappoint(self, qpoint):
        match = self.canvas.snappingUtils().snapToMap(qpoint)
        if match.isValid():
            if self.vertex is None:
                self.vertex = QgsVertexMarker(self.canvas)
                self.vertex.setIconSize(12)
                self.vertex.setPenWidth(2)
                self.vertex.setColor(self.snapcolor)
                self.vertex.setIconType(QgsVertexMarker.ICON_BOX)
            self.vertex.setCenter(match.point())
            return match.point()  # Returns QgsPointXY
        else:
            self.removeVertexMarker()
            return self.toMapCoordinates(qpoint)  # QPoint input, returns QgsPointXY

    def canvasMoveEvent(self, event):
        """Capture the coordinate as the user moves the mouse over
        the canvas."""
        self.snappoint(event.originalPixelPoint())  # input is QPoint

    def canvasReleaseEvent(self, event):
        """Capture the coordinate when the mouse button has been released,
        format it, and copy it to the clipboard. pt is QgsPointXY"""
        pt = self.snappoint(event.originalPixelPoint())
        self.removeVertexMarker()

        try:
            canvasCRS = self.canvas.mapSettings().destinationCrs()
            transform = QgsCoordinateTransform(
                canvasCRS, epsg4326, QgsProject.instance()
            )
            pt4326 = transform.transform(pt.x(), pt.y())
            self.capturePoint.emit(pt4326)
        except Exception:
            pass

    def removeVertexMarker(self):
        if self.vertex is not None:
            self.canvas.scene().removeItem(self.vertex)
            self.vertex = None
