# -*- coding: utf-8 -*-
"""
/***************************************************************************
 ImageInterpretation
                                 A QGIS plugin
 Tool to check input geometry of interpretation and automate the fiilling of fields
 Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
                              -------------------
        begin                : 2019-11-08
        git sha              : $Format:%H$
        copyright            : (C) 2019 by David Kerr
        email                : dk@tcarta.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 pathlib import Path
import pandas as pd
import sys
import os.path

from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction
from qgis.core import QgsProject, QgsVectorLayer, QgsVectorLayerJoinInfo, QgsProject, QgsExpression, Qgis, QgsField
from PyQt5.QtWidgets import QAbstractItemView, QAction, QMessageBox
from PyQt5.QtCore import QVariant


# Initialize Qt resources from file resources.py
from .resources import *
# Import the code for the dialog
from .image_interpretation_dialog import ImageInterpretationDialog

BASE_DIR = Path(__file__).resolve().parent
CSV = BASE_DIR.joinpath('csv/classes.csv')
CSV_POINTS = BASE_DIR.joinpath('csv/classes_for_points.csv')
CSV_AREAS = BASE_DIR.joinpath('csv/hatitat_tile_intersection_areas.csv')


class ImageInterpretation:
    """QGIS Plugin Implementation."""

    def __init__(self, iface):
        """Constructor.

        :param iface: An interface instance that will be passed to this class
            which provides the hook by which you can manipulate the QGIS
            application at run time.
        :type iface: QgsInterface
        """
        # Save reference to the QGIS interface
        self.iface = iface
        # initialize plugin directory
        self.plugin_dir = os.path.dirname(__file__)
        # initialize locale
        locale = QSettings().value('locale/userLocale')[0:2]
        locale_path = os.path.join(
            self.plugin_dir,
            'i18n',
            'ImageInterpretation_{}.qm'.format(locale))

        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)
            QCoreApplication.installTranslator(self.translator)

        # Declare instance attributes
        self.actions = []
        self.menu = self.tr(u'&Interpretation Field Filling')

        # Check if plugin was started the first time in current QGIS session
        # Must be set in initGui() to survive plugin reloads
        self.first_start = None

    # noinspection PyMethodMayBeStatic
    def tr(self, message):
        """Get the translation for a string using Qt translation API.

        We implement this ourselves since we do not inherit QObject.

        :param message: String for translation.
        :type message: str, QString

        :returns: Translated version of message.
        :rtype: QString
        """
        # noinspection PyTypeChecker,PyArgumentList,PyCallByClass
        return QCoreApplication.translate('ImageInterpretation', message)


    def add_action(
        self,
        icon_path,
        text,
        callback,
        enabled_flag=True,
        add_to_menu=True,
        add_to_toolbar=True,
        status_tip=None,
        whats_this=None,
        parent=None):
        """Add a toolbar icon to the toolbar.

        :param icon_path: Path to the icon for this action. Can be a resource
            path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
        :type icon_path: str

        :param text: Text that should be shown in menu items for this action.
        :type text: str

        :param callback: Function to be called when the action is triggered.
        :type callback: function

        :param enabled_flag: A flag indicating if the action should be enabled
            by default. Defaults to True.
        :type enabled_flag: bool

        :param add_to_menu: Flag indicating whether the action should also
            be added to the menu. Defaults to True.
        :type add_to_menu: bool

        :param add_to_toolbar: Flag indicating whether the action should also
            be added to the toolbar. Defaults to True.
        :type add_to_toolbar: bool

        :param status_tip: Optional text to show in a popup when mouse pointer
            hovers over the action.
        :type status_tip: str

        :param parent: Parent widget for the new action. Defaults None.
        :type parent: QWidget

        :param whats_this: Optional text to show in the status bar when the
            mouse pointer hovers over the action.

        :returns: The action that was created. Note that the action is also
            added to self.actions list.
        :rtype: QAction
        """

        icon = QIcon(icon_path)
        action = QAction(icon, text, parent)
        action.triggered.connect(callback)
        action.setEnabled(enabled_flag)

        if status_tip is not None:
            action.setStatusTip(status_tip)

        if whats_this is not None:
            action.setWhatsThis(whats_this)

        if add_to_toolbar:
            # Adds plugin icon to Plugins toolbar
            self.iface.addToolBarIcon(action)

        if add_to_menu:
            self.iface.addPluginToMenu(
                self.menu,
                action)

        self.actions.append(action)

        return action

    def initGui(self):
        """Create the menu entries and toolbar icons inside the QGIS GUI."""

        icon_path = ':/plugins/image_interpretation/icon.png'
        self.add_action(
            icon_path,
            text=self.tr(u'Image interpretation field filler'),
            callback=self.run,
            parent=self.iface.mainWindow())

        # will be set False in run()
        self.first_start = True


    def unload(self):
        """Removes the plugin menu item and icon from QGIS GUI."""
        for action in self.actions:
            self.iface.removePluginMenu(
                self.tr(u'&Interpretation Field Filling'),
                action)
            self.iface.removeToolBarIcon(action)

    def geometry_okay(self, layer, folder):
        """Returns true if geometry good; False if not"""
        geom_okay = True
        overlaps_csv = folder.joinpath(f'overlapping_geometries_{layer.name()}.csv')
        invalid_csv = folder.joinpath(f'invalid_geometries_{layer.name()}.csv')
        df_dict = {'ORTHOID': [], 'ID': [], 'RELATED_ID': [], 'PROBLEM': [], 'CHECKED': [], 'AREA': [], 'CRITICAL': [], 'LOCATION': []}
        invalid_dict = {'ORTHOID': [], 'ID': [], 'CHECKED': []}
        for index, feature in enumerate(layer.getFeatures()):
            for y_feature in layer.getFeatures():
                if not y_feature.id() == feature.id():
                    if y_feature.geometry().overlaps(feature.geometry()):
                        intersection = y_feature.geometry().intersection(feature.geometry())
                        df_dict['ORTHOID'].append(feature[0])
                        df_dict['ID'].append(feature.id())
                        df_dict['RELATED_ID'].append(y_feature.id())
                        df_dict['PROBLEM'].append('Overlapping Geometries')
                        df_dict['AREA'].append(intersection.area())
                        df_dict['LOCATION'].append(intersection.asWkt())
                        df_dict['CHECKED'].append('False')
                        if intersection.area() > 0.05:
                            #print(feature[5], feature.id())
                            #print(y_feature[5], feature.id())
                            #print(f'There is an intersection between polygons. INTERSECTION AREA: {intersection.area()} between {y_feature[1]} and {feature[1]}')
                            df_dict['CRITICAL'].append('True')
                            geom_okay = True
                        else:
                            df_dict['CRITICAL'].append('False')
                    if feature.geometry().contains(y_feature.geometry()):
                        intersection = y_feature.geometry().intersection(feature.geometry())
                        #print(f'Row {feature[1]} ({feature.id()}) contains {y_feature[1]} {feature.id()}')
                        self.iface.messageBar().pushMessage("Error", f'Row {feature[1]} contains {y_feature[1]}', level=Qgis.Critical)
                        df_dict['ORTHOID'].append(feature[0])
                        df_dict['ID'].append(feature.id())
                        df_dict['RELATED_ID'].append(y_feature.id())
                        df_dict['PROBLEM'].append('One geometry contains another')
                        df_dict['AREA'].append(intersection.area())
                        df_dict['LOCATION'].append(intersection.asWkt())
                        df_dict['CHECKED'].append('False')
                        df_dict['CRITICAL'].append('True')
                        geom_okay = False
            if not feature.geometry().isGeosValid():
                feature.geometry().makeValid()
                if not feature.geometry().isGeosValid():
                    print('COULD NOT MAKE VALID')
                    invalid_dict['ORTHOID'].append(feature[0])
                    invalid_dict['ID'].append(feature.id())
                    invalid_dict['CHECKED'].append('False')
                    #self.iface.messageBar().pushMessage("Error", f"There seems to be an invalid geometry in row {feature.id()}; Id {feature[1]}. Please fix this geometry before continuing. Try Fix geometries or repair geometries in ArcPro.", level=Qgis.Critical)
                    geom_okay = True
        if df_dict['ID']:
            df = pd.DataFrame(data=df_dict)
            df.to_csv(overlaps_csv, index=False)
        if invalid_dict['ID']:
            df_invalid = pd.DataFrame(data=invalid_dict)
            df_invalid.to_csv(invalid_csv, index=False)
        return geom_okay

    def complete_fields(self, layer):
        """Join shapefile to csv and fill in the missing fields based on sub ID"""
        infoLyr = QgsVectorLayer(f'file:///{str(CSV)}?delimiter=,','classes','delimitedtext')
        QgsProject.instance().addMapLayer(infoLyr)
        csv_field = 'Int_SubNum'
        shp_field = 'HabitatSub'
        joinObject = QgsVectorLayerJoinInfo()
        joinObject.setJoinFieldName(csv_field)
        joinObject.setTargetFieldName(shp_field)
        joinObject.setJoinLayerId(infoLyr.id())
        joinObject.setUsingMemoryCache(True)
        joinObject.setJoinLayer(infoLyr)
        layer.addJoin(joinObject)
        layer.startEditing()
        ha_areas = []
        for feature in layer.getFeatures():
            shp_fields = ['HabitatTyp', 'HabitatT_1', 'HabitatS_1', 'MMU_HA']
            csv_fields = ['classes_Int_num', 'classes_Int_cls', 'classes_Int_SubCls', 'classes_MMU_']
            for shp, csv in zip(shp_fields, csv_fields):
                f = layer.fields().indexFromName(csv)
                f_ = layer.fields().indexFromName(shp)
                layer.changeAttributeValue(feature.id(), f_, feature[f])
            area_km = layer.fields().indexFromName('Area_KM')
            area_ha = layer.fields().indexFromName('Area_HA')
            orthoid_field = layer.fields().indexFromName('OrthoID')
            id_field = layer.fields().indexFromName('Id')
            layer.changeAttributeValue(feature.id(), area_km, feature.geometry().area() / 10**6)
            layer.changeAttributeValue(feature.id(), area_ha, feature.geometry().area() / 10**4)
            ha_areas.append(feature.geometry().area() / 10**4)
            layer.changeAttributeValue(feature.id(), orthoid_field, feature[0])
            layer.changeAttributeValue(feature.id(), id_field, feature.id())
            
        layer.removeJoin(infoLyr.id())
        QgsProject.instance().removeMapLayer(infoLyr)
        fields_to_drop = set(['RuleID', 'Shape_Leng', 'Shape_Area'])
        field_ids = []
        for field in layer.fields():
            if field.name() in fields_to_drop:
                field_ids.append(layer.fields().indexFromName(field.name()))
        layer.dataProvider().deleteAttributes(field_ids)
        layer.commitChanges()

    def invalid_values_in_shape_attributes(self, layer):
        """Returns True if NULL values found in habitat class, meaning incorrect subclass IDs were entered"""
        values_invalid = False
        hab_type = layer.fields().indexFromName('HabitatTyp')
        hab_id = layer.fields().indexFromName('HabitatT_1')
        hab_sub = layer.fields().indexFromName('HabitatS_1')
        mmu = layer.fields().indexFromName('MMU_HA')

        for i in layer.getFeatures():
            if not i[hab_type] or not i[hab_id] or not i[hab_sub] or not i[mmu]:
                values_invalid = True
        return values_invalid

    def area_larger_than_mmu(self, layer, folder):
        """Returns True if polygons smaller than MMU, along with list of ids which fall below threshold"""
        ids_where_mmu_smaller = []
        greater_than_mmu = True
        area_ha = layer.fields().indexFromName('Area_HA')
        mmu_ha = layer.fields().indexFromName('MMU_HA')
        check_mmu_areas_csv = folder.joinpath(f'check_mmu_log_{layer.name()}.csv')
        df_dict = {'ID': [], 'CHECKED': [], 'AREA': [], 'MMU': []}
        for feature in layer.getFeatures():
            if feature[area_ha] < feature[mmu_ha]:
                greater_than_mmu = False
                ids_where_mmu_smaller.append(feature.id())
                df_dict['ID'].append(feature[1])
                df_dict['CHECKED'].append('False')
                df_dict['AREA'].append(feature[area_ha])
                df_dict['MMU'].append(feature[mmu_ha])
        if df_dict['ID']:
            df = pd.DataFrame(data=df_dict)
            df.to_csv(check_mmu_areas_csv, index=False)
        return greater_than_mmu, ids_where_mmu_smaller

    def update_mmu_valid_field(self, layer):
        """Sets mmu_valid_field to false if mmu > area_ha"""
        area_ha = layer.fields().indexFromName('Area_HA')
        mmu_ha = layer.fields().indexFromName('MMU_HA')
        caps = layer.dataProvider().capabilities()
        if layer.fields().indexFromName('mmu_valid') == -1:
            if caps:
                greater_than_mmu_field = QgsField('mmu_valid', QVariant.String)
                if not greater_than_mmu_field in layer.fields():
                    layer.dataProvider().addAttributes([greater_than_mmu_field])
                    layer.updateFields()
        idx = layer.fields().indexFromName('mmu_valid')
        layer.startEditing()
        for feature in layer.getFeatures():
            if feature[area_ha] < feature[mmu_ha]:
                layer.changeAttributeValue(feature.id(), idx, 'False')      
            else:
                layer.changeAttributeValue(feature.id(), idx, 'True')
            layer.updateFields()
        layer.commitChanges()

    def total_area_matches_expected_area(self, layer):
        """Returns True if summed area of polygons matches area of habitat's intersection with tile --> Threshold: 0.000005% (0.5 sq metres in 10,000ha)"""
        areas_match = True
        df = pd.read_csv(CSV_AREAS)
        orthoid_index = layer.fields().indexFromName('OrthoID')
        area_shp_index = layer.fields().indexFromName('Area_HA')
        area_of_all_polygons = []
        for feature in layer.getFeatures():
            orthoid = feature[orthoid_index]
            area_shp = feature[area_shp_index]
            area_of_all_polygons.append(area_shp)
        threshold_area = df['Int_area'][df['index'] == orthoid].values[0] * 0.000000005# <----- 0.000005% total area
        if abs(df['Int_area'][df['index'] == orthoid].values[0] - sum(area_of_all_polygons)) > threshold_area:
            areas_match = False
            print(f'SUM: {sum(area_of_all_polygons):.2f}')
            print(f'EXPECTED: {abs(df["Int_area"][df["index"] == orthoid].values[0]):.2f}')
        sum_area = sum(area_of_all_polygons)
        expected_area = abs(df["Int_area"][df["index"] == orthoid].values[0])
        return areas_match, sum_area, expected_area

    def join_to_pt_and_extract(self, layer):
        """Extract interpretation values to points"""
        print(layer.name())
        pt_name = f'{layer.name()}_points'
        try:
            pt_lyr = QgsProject.instance().mapLayersByName(pt_name)[0]
        except IndexError:
            QMessageBox.critical(self.iface.mainWindow(), "WARNING!", f'Please add the correct point shapefile to the map. Could not find {pt_name}')
            raise
        shp_fields = ['HabitatTyp', 'HabitatT_1', 'HabitatS_1', 'HabitatSub']
        pt_fields = ['Hab_num', 'Hab_name', 'Hab_subnam', 'Hab_subnum']
        pt_lyr.startEditing()
        for feature_x in pt_lyr.getFeatures():
            for feature_y in layer.getFeatures():
                if feature_y.geometry().intersects(feature_x.geometry()):
                    for shp, pt in zip(shp_fields, pt_fields):
                        f = pt_lyr.fields().indexFromName(pt)
                        f_ = layer.fields().indexFromName(shp)
                        pt_lyr.changeAttributeValue(feature_x.id(), f, feature_y[f_])
        pt_lyr.commitChanges()

    def update_mmu_valid_where_intersect_border(self, layer, folder):
        border_shp = [x for x in folder.iterdir() if x.name.endswith('_border.shp')][0]
        #mmu_csv = [x for x in folder.iterdir() if x.name.startswith(f'check_mmu_log')][0]
        mmu_csv = folder.joinpath(f'check_mmu_log_{layer.name()}.csv')
        border_lyr = QgsProject.instance().addMapLayer(QgsVectorLayer(str(border_shp)))
        mmu_valid_field = 'mmu_valid'
        f_ = layer.fields().indexFromName(mmu_valid_field)
        layer.startEditing()
        for feature_x in layer.getFeatures():
            for feature_y in border_lyr.getFeatures():
                if feature_x.geometry().intersects(feature_y.geometry()):
                    layer.changeAttributeValue(feature_x.id(), f_, 'True')
                    if mmu_csv.exists():
                        df = pd.read_csv(mmu_csv).set_index('ID')
                        if df.index.contains(feature_x.id()):
                            df['CHECKED'].loc[feature_x.id()] = 'True'
                            print(df['CHECKED'].loc[feature_x.id()])
                        df.to_csv(mmu_csv)
        layer.commitChanges()
        
        QgsProject.instance().removeMapLayer(border_lyr)





    def check_inputs_and_fill_fields(self, layer):
        """Main function to carry out logic and call helper functions"""
        #check if geometry in added layers is valid
        filename = Path(layer.source().split('|')[0]).resolve()
        if filename.exists():
            folder = filename.parent
            self.complete_fields(layer)
            if self.geometry_okay(layer, folder):
                if self.invalid_values_in_shape_attributes(layer):
                    QMessageBox.critical(self.iface.mainWindow(), "WARNING!", f'There are invalid Habitat codes in the table. Please check the numeric subclass code in rows that have empty values.')
                mmu_area_okay, ids_where_mmu_smaller = self.area_larger_than_mmu(layer, folder)
                if not mmu_area_okay:
                    QMessageBox.critical(self.iface.mainWindow(), "WARNING!", f'The following IDs have areas smaller that the MMU. The file will still be saved, but these areas should be checked in IDs: {ids_where_mmu_smaller}. Please check check_mmu_log_{layer.name()}.csv for details.')
                self.update_mmu_valid_field(layer)
                self.update_mmu_valid_where_intersect_border(layer, folder)
                areas_match, sum_area, expected_area = self.total_area_matches_expected_area(layer)
                if not areas_match:
                    QMessageBox.critical(self.iface.mainWindow(),
                            'Error with Areas!',
                            f"The sum of interpretted areas is different to the total area of this tile. There may by errors/gaps in the polygons if no changes have been made to the external border. Sum area={sum_area:.2f} :: Expected area={expected_area:.2f}. This file will still be saved but please check the reason for the difference in areas before continuing. There may be slivers or gaps between geometries")
                self.join_to_pt_and_extract(layer)
                self.iface.messageBar().pushMessage("Success", "Shapefile saved successfully.")
                QMessageBox.information(self.iface.mainWindow(),
                            'Success',
                            "Shapefile saved successfully.")        
            else:
                QMessageBox.critical(self.iface.mainWindow(),
                            'Error! Something went wrong',
                            "Shapefile NOT saved. Please check Python terminal or Message Log for details. You may need to run the 'FIX GEOMETRIES TOOL' or remove some overlapped polygons.")
        else:
            QMessageBox.critical(self.iface.mainWindow(),
                            'Error! Layer not saved',
                            "Please save the selected layer before continuing.")


    def run(self):
        """Run method that performs all the real work"""

        # Create the dialog with elements (after translation) and keep reference
        # Only create GUI ONCE in callback, so that it will only load when the plugin is started
        if self.first_start == True:
            self.first_start = False
            self.dlg = ImageInterpretationDialog()

         # Fetch the currently loaded layers
        layers = QgsProject.instance().layerTreeRoot().children()
        # Clear the contents of the comboBox from previous runs
        self.dlg.comboBox.clear()
        # Populate the comboBox with names of all the loaded layers
        self.dlg.comboBox.addItems([layer.name() for layer in layers])
        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result:
            # Do something useful here - delete the line containing pass and
            # substitute with your code.            
            selected_layer_index = self.dlg.comboBox.currentIndex()
            selected_layer = layers[selected_layer_index].layer()
            self.check_inputs_and_fill_fields(selected_layer)
