# -*- coding: utf-8 -*-

"""
/***************************************************************************
 AgredateAdmbyMesh
                                 A QGIS plugin
 Read a CSV file and calculate the number of lines for each item in the specified column
 Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
                              -------------------
        begin                : 2020-05-15
        copyright            : (C) 2020 by Yoichi Kayama
        email                : yoichi.kayama@gmail.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.                                   *
 *                                                                         *
 ***************************************************************************/
"""

__author__ = 'Yoichi Kayama'
__date__ = '2020-12-11'
__copyright__ = '(C) 2020 by Yoichi Kayama'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from qgis.PyQt.QtCore import ( QCoreApplication ,
                           QVariant)

from qgis.core import (QgsProcessing,
                       QgsFeatureSink,
                       QgsProcessingException,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterEnum,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterFeatureSink,
                       QgsProcessingParameterVectorDestination,
                       QgsProcessingParameterFile,
                       QgsProcessingParameterVectorLayer,
                       QgsProcessingParameterBoolean,
                       QgsProcessingParameterField,
                       QgsProcessingParameterNumber,
                       QgsProcessingOutputVectorLayer,
                       QgsVirtualLayerDefinition,
                       QgsVectorLayer,
                       QgsProcessingUtils,
                       QgsProcessingMultiStepFeedback,
                       QgsProject,
                       QgsWkbTypes,
                       QgsExpression,
                       QgsField,
                       QgsFields,
                       QgsFeature,
                       QgsFeatureRequest,
                       QgsProcessingParameterString)


import processing
import sqlite3
import csv

from .mod import  agtools

class AggregateAdmbyMeshAlgorithm(QgsProcessingAlgorithm):
    """
    This is an example algorithm that takes a vector layer and
    creates a new identical one.

    It is meant to be used as an example of how to create your own
    algorithms and explain methods and variables used to do it. An
    algorithm like this will be available in all elements, and there
    is not need for additional work.

    All Processing algorithms should extend the QgsProcessingAlgorithm
    class.
    """

    # Constants used to refer to parameters and outputs. They will be
    # used when calling the algorithm from another algorithm, or when
    # calling from the QGIS console.

    OUTPUT = 'OUTPUT'
    INPUT = 'INPUT'

    #ENCODING = 'ENCODING'

    #encode = ['SJIS','UTF-8']

    proportional_div = ['人口','面積']


    def initAlgorithm(self, config):
        """
        Here we define the inputs and output of the algorithm, along
        with some other properties.
        """
        self.addParameter(
            QgsProcessingParameterVectorLayer(
                self.INPUT,
                '入力レイヤ'
            )
         )

        self.addParameter(QgsProcessingParameterField('aggrefield', '集計値フィールド', 
                         type=QgsProcessingParameterField.Numeric, parentLayerParameterName=self.INPUT, optional=False, allowMultiple=False, defaultValue=None))
        

        self.addParameter(QgsProcessingParameterVectorLayer('meshlayer', 'メッシュレイヤ',
                         types=[QgsProcessing.TypeVectorPolygon], defaultValue=None))

        self.addParameter(QgsProcessingParameterField('meshid', 'メッシュIDフィールド', 
                         type=QgsProcessingParameterField.String, parentLayerParameterName='meshlayer', optional=False, allowMultiple=False))



 
        #self.addParameter(QgsProcessingParameterNumber('limit_sample', '最小サンプル数',
        #                  defaultValue=3))

        #self.addParameter(QgsProcessingParameterNumber('maxdivide', '最大分割回数',
        #                  defaultValue=8))

        #self.addParameter(QgsProcessingParameterBoolean('uneven_div', '不均等分割',
        #                  defaultValue=False))

        #self.addParameter(QgsProcessingParameterNumber('current_div', 'カレント分割回数',
        #                  defaultValue=1))



        #  propotinal division method
        #propParam = QgsProcessingParameterEnum(
        #        "PROPDIV",
        #        self.tr('按分方法選択')
        #    )

        #propParam.setOptions(self.proportional_div)
        #propParam.setAllowMultiple(False)
        #propParam.setDefaultValue(QVariant('人口'))
        #  file encoding
        #self.addParameter(
        #    propParam
        #)

        #self.addParameter(QgsProcessingParameterVectorLayer('poplayer', '人口レイヤ',
        #                 types=[QgsProcessing.TypeVectorPolygon], optional=True,  defaultValue=None))
        #self.addParameter(QgsProcessingParameterField('popfield', '人口フィールド', 
        #                 type=QgsProcessingParameterField.String, parentLayerParameterName='addresslayer', optional=True, allowMultiple=False, defaultValue=None))
        


        # We add a feature sink in which to store our processed features (this
        # usually takes the form of a newly created vector layer when the
        # algorithm is run in QGIS).
        self.addParameter(
            QgsProcessingParameterFeatureSink(
                self.OUTPUT,
                self.tr('Output layer')
            )
        )

    def processAlgorithm(self, parameters, context, model_feedback):
        """
        Here is where the processing itself takes place.
        """
        results = {}

        feedback = QgsProcessingMultiStepFeedback(1, model_feedback)

        inputLayer = self.parameterAsVectorLayer(
            parameters,
            self.INPUT,
            context
        )
        if inputLayer  is None:
            raise QgsProcessingException(self.tr('input layer missed'))



        meshLayer = self.parameterAsVectorLayer(
            parameters,
            "meshlayer",
            context
        )
        if meshLayer  is None:
            raise QgsProcessingException(self.tr('mesh layer missed'))

        meshidfields = self.parameterAsFields  (
             parameters,
             'meshid',
             context
        )


        aggrefields = self.parameterAsFields  (
             parameters,
             'aggrefield',
             context
        )



        #limit_sample = self.parameterAsInt ( parameters,
        #     'limit_sample',
        #     context)

      

        feedback.setCurrentStep(1)
        if feedback.isCanceled():
            return {}

        #  InterSect
 

        #Stat_CSVAddressPolygon

    
    #   行政界の面積計算
    #    
    #  面積出力フィールド名

        area_column = 'adm_area'

        params3 = { 'INPUT' :  inputLayer, 'FIELD_NAME' : area_column , 'FIELD_TYPE': 0, 'FIELD_LENGTH':12, 'FIELD_PRECISION':5, 
                 'NEW_FIELD':1,'FORMULA':'$area','OUTPUT' :QgsProcessing.TEMPORARY_OUTPUT }

        res3 = processing.run('qgis:fieldcalculator', params3, context=context, feedback=feedback ,is_child_algorithm=True)

        if feedback.isCanceled():
            return {}

        feedback.pushConsoleInfo( "caluculate area OK "  )

    #   ここから関数化がいいかも
    #   メッシュと行政界のIntesect
     
        params2 = { 'INPUT' : meshLayer, 'INPUT_FIELDS' : [], 
                'OUTPUT' : QgsProcessing.TEMPORARY_OUTPUT, 'OVERLAY' : res3["OUTPUT"], 'OVERLAY_FIELDS' : [] }
                 #             'OUTPUT' : parameters["OUTPUT"], 'OVERLAY' : res3["OUTPUT"], 'OVERLAY_FIELDS' : [] }

#      ここはIntersectではなくて union
        #res2 = processing.run('native:intersection', params2,  context=context, feedback=feedback ,is_child_algorithm=True)
        res2 = processing.run('qgis:union', params2,  context=context, feedback=feedback ,is_child_algorithm=True)
        if feedback.isCanceled():
            return {}

        feedback.pushConsoleInfo( "union  OK "  )
    #   Inter sect ポリゴンの面積計算



        tgLayer = res2["OUTPUT"]

        if type(tgLayer) is str:
            tgLayer =  QgsVectorLayer(tgLayer, "intesect", "ogr")

        #tgLayer.beginEditCommand("Feature triangulation")

        ad_areacolumn = 'isect_area'
        ratio_column = 'area_ratio'
        anbun_col = 'anbun_colum'


        tgLayer.dataProvider().addAttributes([QgsField(ad_areacolumn, QVariant.Double),QgsField(ratio_column, QVariant.Double),QgsField(anbun_col,QVariant.Double)])
        tgLayer.updateFields()

        newFlag = False
        
        params4 = { 'INPUT' : res2["OUTPUT"], 'FIELD_NAME' : ad_areacolumn , 'FIELD_TYPE': 0, 'FIELD_LENGTH':12, 
        #             'FIELD_PRECISION':5, 'NEW_FIELD':1,'FORMULA':'$area','OUTPUT' :parameters["OUTPUT"] }
                    'FIELD_PRECISION':5, 'NEW_FIELD':newFlag,'FORMULA':'$area','OUTPUT' :QgsProcessing.TEMPORARY_OUTPUT }    


        #for feat  in tgLayer.getFeatures():

            #feat[ad_areacolumn] = feat.geometry().area()
        #    feedback.pushConsoleInfo( "feature "+ str(feat[0]) )
        #    feedback.pushConsoleInfo( "area "+ str(feat.geometry().area())  )
            #tgLayer.updateFeature(feat)
        
        #tgLayer.endEditCommand()

        #results["OUTPUT"] = res2["OUTPUT"]

        #return results



        #params4 = { 'INPUT' : res2["OUTPUT"], 'FIELD_NAME' : ad_areacolumn , 'FIELD_TYPE': 0, 'FIELD_LENGTH':12, 
        #             'FIELD_PRECISION':5, 'NEW_FIELD':1,'FORMULA':'$area','OUTPUT' :parameters["OUTPUT"] }
        #            'FIELD_PRECISION':5, 'NEW_FIELD':1,'FORMULA':'$area','OUTPUT' :QgsProcessing.TEMPORARY_OUTPUT }

        res5 = processing.run('qgis:fieldcalculator', params4,  context=context, feedback=feedback ,is_child_algorithm=True)
        if feedback.isCanceled():
            return {}
        feedback.pushConsoleInfo( "calc area ok " )


        ratio_str = ad_areacolumn + "/" + area_column
        params5 = { 'INPUT' : res5["OUTPUT"], 'FIELD_NAME' :  ratio_column , 'FIELD_TYPE': 0, 'FIELD_LENGTH':12, 
         #            'FIELD_PRECISION':5, 'NEW_FIELD':False,'FORMULA':ratio_str,'OUTPUT' :parameters["OUTPUT"] }
                    'FIELD_PRECISION':5, 'NEW_FIELD':newFlag,'FORMULA':ratio_str,'OUTPUT' :QgsProcessing.TEMPORARY_OUTPUT }   
        res6 = processing.run('qgis:fieldcalculator', params5,  context=context, feedback=feedback ,is_child_algorithm=True)
        if feedback.isCanceled():
            return {}
        feedback.pushConsoleInfo( "calc ratio ok " )

      


     # Intersect ポリゴンと元の行政界ポリゴンの面積比とサンプル数値をかけてInterSectポリゴン単位の案分サンプル値を作成する
     #  def CalcDataUsingRatio(  intersect_output, area_column,ratio_column , out_table, ad_areacolumn)
     #

     #   intersect_output    Intersect 結果
     #   area_column   面積出力カラム名
     #   ratio_column   按分集計値出力カラム名
     #   out_table     出力テーブル名
     #  ad_areacolumn        行政界ポリゴンテーブルの面積値格納カラム名
     #
     #   ratio_column = 'area_ratio'

     #   res5 = agtools.CalcDataUsingRatio(  res4['OUTPUT'], area_column, ratio_column ,  ad_areacolumn , model_feedback)


     #  按分数値算出

        anbun_col = 'snum'

        formula_str = aggrefields[0] + " * " + ratio_column

        params7 = { 'INPUT' : res6["OUTPUT"], 'FIELD_NAME' : anbun_col , 'FIELD_TYPE': 0, 'FIELD_LENGTH':12, 
                    # 'FIELD_PRECISION':5, 'NEW_FIELD':newFlag,'FORMULA':formula_str ,'OUTPUT' :parameters["OUTPUT"]  }
                               'FIELD_PRECISION':5, 'NEW_FIELD':newFlag,'FORMULA':formula_str ,'OUTPUT' :QgsProcessing.TEMPORARY_OUTPUT }

        res7 = processing.run('qgis:fieldcalculator', params7,   context=context, feedback=feedback )

        #        res7 = processing.run('qgis:fieldcalculator', params7,  context=context, feedback=feedback ,is_child_algorithm=True)
        if feedback.isCanceled():
            return {}
        feedback.pushConsoleInfo( "anbun ok " )
     #   results["OUTPUT"] = res7["OUTPUT"]
      #  return results

    #   按分数値をもとにメッシュ別集計
        meshid_f = meshidfields[0]

        #QgsProject.instance().addMapLayer(res6["OUTPUT"])

        #mesh_aggregate = 'aggregate(layer:=\'' + res6["OUTPUT"].id() + '\',aggregate:=\'sum\',expression:="'+ anbun_col + '", filter:="' + meshid_f +'"=attribute(@parent,\'' + meshid_f + '\'))'
        #feedback.pushConsoleInfo( "mesh_aggregate " + mesh_aggregate )

        #mesh_exr = QgsExpression( mesh_aggregate )
        #params6 = { 'INPUT' : meshLayer, 'FIELD_NAME' : anbun_col , 'FIELD_TYPE': 1,  
        #              'NEW_FIELD':1,'FORMULA':mesh_exr ,'OUTPUT' :QgsProcessing.TEMPORARY_OUTPUT }

        agar = []


        tgLayer = res7["OUTPUT"]

   

        if type(tgLayer) is str:
            tgLayer =  QgsVectorLayer(tgLayer, "intesect", "ogr")

        for field in tgLayer.fields():

            agreg = {}

            agreg['input'] = '"' + field.name() + '"'


            feedback.pushConsoleInfo( "name "  + field.name() )
            agreg['name']  = field.name()
            agreg['aggregate'] = 'first_value'

            agreg['length'] =field.length()
            agreg['precision'] =field.precision()     
            agreg['type'] =field.type()   

            if field.name() == anbun_col :
                 agreg['aggregate'] = 'sum'

            agar.append(agreg)



        
        params6 = { 'INPUT' : res7["OUTPUT"], 'GROUP_BY' : meshid_f, 'AGGREGATES': agar, 'OUTPUT' :QgsProcessing.TEMPORARY_OUTPUT }
        feedback.pushConsoleInfo( "aggregate "  )
        res8 = processing.run('qgis:aggregate', params6,  context=context, feedback=feedback ,is_child_algorithm=True)

        if feedback.isCanceled():
            return {}
        feedback.pushConsoleInfo( "aggregate OK "  )

    #   レイヤ結合　　qgis:joinattributestable
        #QgsProject.instance().addMapLayer(res7["OUTPUT"])

        param7 = { 'DISCARD_NONMATCHING' : False, 'FIELD' : meshid_f, 'FIELDS_TO_COPY' : [anbun_col], 'FIELD_2' : meshid_f, 
             'INPUT' : meshLayer, 
             'INPUT_2' : res8['OUTPUT'], 'METHOD' : 1, 'OUTPUT' : parameters["OUTPUT"], 'PREFIX' : '' }



        res9 = processing.run('qgis:joinattributestable', param7,  context=context, feedback=feedback ,is_child_algorithm=True)

        if feedback.isCanceled():
            return {}
        feedback.pushConsoleInfo( "joinattributetable OK"  )



      #  divide_f = "divide_f"



      #  params8 = { 'INPUT' : res8["OUTPUT"], 'FIELD_NAME' : divide_f , 'FIELD_TYPE': 1,  
      #                'NEW_FIELD':1,'FORMULA':QgsExpression('0') ,'OUTPUT' :QgsProcessing.TEMPORARY_OUTPUT }

      #  res9 = processing.run('qgis:fieldcalculator', params8, feedback=feedback)


      #  if feedback.isCanceled():
      #      return {}
      #  feedback.pushConsoleInfo( "add divide flag OK"  )

    


     #  最低値チェック
     #  ここが < なのか <= なのかはチェックが必要
        #exp_str = '"' + anbun_col + '" <= ' + str(limit_sample ) + ' and "' + divide_f + '"=0'

        #exp_str = '"' + anbun_col + '" <= ' + str(limit_sample ) 
        #feedback.pushConsoleInfo( "exp_str " + exp_str )
        #nexpression  =QgsExpression(exp_str)

        #request = QgsFeatureRequest().setFilterExpression(exp_str)


        #rsLayer = res9["OUTPUT"]

        #if type(rsLayer) is str:
       #     rsLayer =  QgsVectorLayer(rsLayer, "mesh", "memory")

        #tgLayer.beginEditCommand("Feature triangulation")


       # matches = 0
        #for f in rsLayer.getFeatures():
       #      feedback.pushConsoleInfo( "value  " +str( f[anbun_col])  )
             #matches += 1

        #feedback.pushConsoleInfo( "make expression OK"  )
        #params10 =  { 'INPUT' : res8["OUTPUT"], 'EXPRESSION' : expression , 'METHOD': 0  }
        #outputs_statv = processing.run('QGIS_stat:Stat_CSVAddressPolygon', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

        #res11 = processing.run('qgis:selectbyexpression', params10, feedback=feedback)

        # 最低値　に達した地物数の算出
        #scount = res11["OUTPUT"].selectedFeatureCount()

        #feedback.pushConsoleInfo( "scount = "+str(matches)  )

        #if scount > 0:　　　　
        # #  最低値に達したものがある場合
        # 最低値に達したメッシュのフラグを終了に変更


        # Return the results of the algorithm. In this case our only result is
        # the feature sink which contains the processed features, but some
        # algorithms may return multiple feature sinks, calculated numeric
        # statistics, etc. These should all be included in the returned
        # dictionary, with keys matching the feature corresponding parameter
        # or output names.

        results["OUTPUT"] = res9["OUTPUT"]
       # results["LIMITPOL"] = matches

        return  results


    def name(self):
        """
        Returns the algorithm name, used for identifying the algorithm. This
        string should be fixed for the algorithm, and must not be localised.
        The name should be unique within each provider. Names should contain
        lowercase alphanumeric characters only and no spaces or other
        formatting characters.
        """
        return 'AggregateAdmbyMeshAlgorithm'

    def displayName(self):
        """
        Returns the translated algorithm name, which should be used for any
        user-visible display of the algorithm name.
        """
        return '行政界メッシュ集計（面積按分　単一メッシュ)'

    def group(self):
        """
        Returns the name of the group this algorithm belongs to. This string
        should be localised.
        """
        return '集計'

    def groupId(self):
        """
        Returns the unique ID of the group this algorithm belongs to. This
        string should be fixed for the algorithm, and must not be localised.
        The group id should be unique within each provider. Group id should
        contain lowercase alphanumeric characters only and no spaces or other
        formatting characters.
        """
        return self.tr('Aggregate')

    def tr(self, string):
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return AggregateAdmbyMeshAlgorithm()
