from __future__ import absolute_import
from builtins import zip
from builtins import map
from builtins import str
from builtins import range
from PyQt5.QtCore import *

from PyQt5.QtGui import *

from PyQt5 import QtCore, QtGui

from qgis.core import *

from qgis.gui import *

from .Ui_ECOAL import Ui_ECOAL

import csv

import re

import glob

from . import ftools_utils

from . import GdalTools_utils as Utils

import subprocess

from PyQt5 import QtXml

from PyQt5.QtGui import QImage

from PyQt5.QtGui import QApplication, QFont

from PyQt5.QtCore import QFileInfo, QSize, QRectF

from PyQt5.phonon import Phonon

import csv

import re

import os, glob, sys  



# Prepare processing framework

from processing.core.Processing import Processing

Processing.initialize()



class ECOAL(QDialog, Ui_ECOAL):

    

    def __init__(self, iface):

        QDialog.__init__(self)

        self.iface = iface

        self.setupUi(self)

        

        self.selectButton.clicked.connect(self.fillinputdirectory)

        self.buttonBox.accepted.connect(self.txttemperat)

        self.selectmaskButton.clicked.connect(self.fillinputmask)

        self.selectoutButton.clicked.connect(self.filloutputdirectory)

        self.selecttxtButton.clicked.connect(self.fillinputtxt)

        self.selectshpButton.clicked.connect(self.fillinputmask)

        self.play_pause.clicked.connect(self.playClicked)

        

        

    # play video

    def playClicked(self):

        #if self.player.mediaObject().state() == Phonon.PlayingState:

            #self.player.pause()

        #else:

            #self.player.play()       

        #self.player = Phonon.VideoPlayer()

        images_out = self.outputCombo.currentText()

        if os.path.exists(os.path.join(str(images_out), 'movie.mp4'))==True:

            media_source = Phonon.MediaSource(os.path.join(str(images_out), 'movie.mp4'))

            self.player.show()

            self.player.load(media_source)

            self.player.play()   

        else:

            QMessageBox.warning( self, self.tr( "No files found" ), self.tr( "There is no video in this directory. Please create a video first." ) )

            

    def fillinputdirectory(self):

        settings = QSettings()

        lastDir = settings.value( "/fTools/lastRasterDir", "." )

        inDir = QFileDialog.getExistingDirectory( self, self.tr( "Select directory with text files (temperature data)" ), lastDir )

    

        if not inDir:

            return

    

        self.workDir = QDir( inDir )

        #self.workDir.setFilter( QDir.Files | QDir.NoSymLinks | QDir.NoDotAndDotDot )

        self.workDir.setFilter( QDir.Files)

        #nameFilter = [ "*.txt", "*."]

        #fil = self.workDir.setNameFilters( nameFilter )

        self.inputFiles = self.workDir.entryList()

        if len( self.inputFiles ) == 0:

            QMessageBox.warning( self, self.tr( "No files found" ), self.tr( "There are no files in this directory. Please select another one." ) )

            self.inputFiles = None

            return

    

        settings.setValue( "/fTools/lastRasterDir", inDir )

    

        self.inputLayerCombo.addItem( inDir )       

        

    # OUTPUT IMAGES

    def filloutputdirectory(self):

        settings = QSettings()

        lastDir = settings.value( "/fTools/lastRasterDir", "." )

        inDir = QFileDialog.getExistingDirectory( self, self.tr( "Select directory to save images" ), lastDir )

    

        if not inDir:

            return

    

        self.workDir = QDir( inDir )

        self.workDir.setFilter( QDir.Files | QDir.NoSymLinks | QDir.NoDotAndDotDot )

    

        settings.setValue( "/fTools/lastRasterDir", inDir )

    

        self.outputCombo.addItem( inDir )    

        

        

    # INPUT MASK SHAPEFILE

    def fillinputmask(self):

        lastUsedFilter = Utils.FileFilter.lastUsedVectorFilter()

        inputFile, encoding = Utils.FileDialog.getOpenFileName(self, self.tr( "Select the input file" ), Utils.FileFilter.allVectorsFilter(), lastUsedFilter, True)

        self.lastEncoding = encoding

        self.inputmaskCombo.addItem(inputFile)

        check = QFile(inputFile) 

        

    def fillinputtxt(self):

        inputFile = Utils.FileDialog.getOpenFileName()

        self.inputtxtCombo.addItem(inputFile)

        check = QFile(inputFile)           

        

        

    def txttemperat(self):

        

        import os

        diretory_home = os.path.dirname(os.path.realpath(__file__))

        #QMessageBox.about(self,'test', str(diretory_home))

        inf = self.inputLayerCombo.currentText()

        images_out = self.outputCombo.currentText()

        input_txt = self.inputtxtCombo.currentText()

        inputMask = QgsVectorLayer(self.inputmaskCombo.currentText(),'limite','ogr')

             

        extent_rect = inputMask.extent()

            

        xmin = extent_rect.xMinimum()

        xmax = extent_rect.xMaximum()

        ymin = extent_rect.yMinimum()

        ymax = extent_rect.yMaximum()

        extent = str(xmin) + "," + str(xmax) + "," + str(ymin) + "," + str(ymax)          

        

        import os

        for filename in os.listdir(inf):

            if os.path.splitext(os.path.basename(filename))[1] != '.txt':

                #QMessageBox.about(self,'test', str('yes'))

                os.rename(os.path.join(inf,filename), os.path.join(inf, filename + '.txt'))        

        

        strings = ("Position") 

        #QMessageBox.about(self,'test', str('r' + str(inf) + '\*.txt'))

        # remove strings (falta remover nan)

        for file in glob.glob(str(inf) + '\*.txt'): 

            

            input_file = open(file, 'r')

            data = []

            input_file.readline() # first line with titles

            for line in input_file: # lines with data

                data.append(list(map(lambda x: float(x.replace(',', '.'), line.split()))))

            input_file.close()

            

            data.sort(key=lambda x: x[1])

            data = data[1:-1]

            

            result_file = open('result_file', 'w')

            for row in data:

                result_file.writeline('\t'.join(row))

            

            result_file.close()              

            

            

            ##QMessageBox.about(self,'test', str(file))

            #lines = open(file).readlines()

            ##QMessageBox.about(self,'test', str(lines))

            ##print lines

            #with open(file) as myFile:  

                #for num, line in enumerate(myFile, 1): 

                    ##print line

                    ##print lines_

                    ##print columns

                    #if any(s in line for s in strings):  

                        ##print 'yes'

                        ##print num

                        ##lista.append()

                        ##print lista

                        #val = num 

                        ##print num

                    ##else:

                        ##val = 0

            ##print lines[1:]

            #try:

                #val

                #try:  

                    #open(os.path.join(os.path.dirname(inf), 'newfile.txt'), 'w').writelines(lines[val:])

                    ##open(str(inf)+'\newfile.txt', 'w').writelines(lines[val:])  

                    #os.remove(file) 

                    #os.rename(os.path.join(os.path.dirname(inf), 'newfile.txt'), file)

                    ##os.rename(str(inf) + '\newfile.txt', file)  

                #except IndexError:  

                    #pass  

            #except NameError:

                #val = None             

        

        ##QMessageBox.about(self,'test', str('yes'))

        ## replace comma by dot

        #for file in glob.glob(str(inf) + '\*.txt'):

            #input_file = open(file, "r") 

            #output_file = open(os.path.join(os.path.dirname(inf), 'newfile.txt'), "w") 

            #for line in input_file: 

                #line = line.replace(",", ".") 

                #output_file.write(line) 

            #output_file.close()      

            #input_file.close()

            #try:

                #os.remove(file)  

                #os.rename(os.path.join(os.path.dirname(inf), 'newfile.txt'), file)   

            #except IndexError:

                #pass

        

        #listt =[]

        #for file in glob.glob(str(inf) + '\*.txt'):

            #input_file = open(file, "r") 

            #output_file = open(os.path.join(os.path.dirname(inf), 'newfile.txt'), "w") 

            #for lin in input_file: 

                ##print lin[0]

                #line = lin.strip()

                #sline = lin.split()

                ##print sline

                ## list with the values for points

                #if float(sline[0])>=269.99 and float(sline[0])<=935.0551:

                    ##print sline[0]

                    ##listt.append(sline[1])

                    ##output_file.write(sline[1] + '\n')

                    #sline.insert(2,sline[1])

                    #output_file.write("\t".join(sline)+"\n")

            ##print listt

            #output_file.close()      

            #input_file.close()

            #try:

                #os.remove(file)  

                #os.rename(os.path.join(os.path.dirname(inf), 'newfile.txt'), file)   

            #except IndexError:

                #pass    

        

        # create txt file with coordinates and median temperature values to introduce as points in QGIS

        for file in glob.glob(str(inf) + '\*.txt'):

            input_file = open(file, "r") 

            lista =  []

            output_file = open(os.path.join(os.path.dirname(inf), 'newfile.txt'), "w") 

            for lin in input_file:  

                line = lin.strip()

                sline = lin.split()        

                #print sline[1]

                

                lista.append(sline[1])

            #print lista   

            values = [float(x) for x in lista]

            #print type(values[0])

            subList = [values[n:n+10] for n in range(0, len(values), 10)]

            #print type(subList[0][0])

            #print subList[0]

            list_median = []

            for l in subList:

                ##calculate median of each 10 values

                median = (sorted((l))[int(round((len(l) - 1) / 2.0))] + sorted(l)[int(round((len(l) - 1) // 2.0))]) / 2.0

                list_median.append(median)

            #print list_median

            

            #for line in output_file:

                #sline_out = line.split()

                ##print sline_out

                #sline_out.insert(3,str(elem))

                #print sline_out

            #output_file.write("\t".join(sline_out)+"\n") 

            

            # if txt doesn't exist and shp exist

            if self.inputtxtCombo.currentText()=='':

                #QMessageBox.about(self,'test', str('shp exist'))

                shp = self.inputshpCombo.currentText()

                layer_points = QgsVectorLayer(str(shp).encode('utf8'), shp , "ogr")

                iter = layer_points.getFeatures()

                list_tuple_coord = []

                for feature in iter:

                    geom = feature.geometry()

                    if geom.type() == QGis.Point:

                        x = geom.asPoint()

                        list_tuple_coord.append(x)

                #Extract the points coordinates and write in txt file        

                with open(os.path.join(diretory_home, 'shp_points_coord.txt').replace('\\', '/'), 'w') as f:

                    for line in list_tuple_coord:

                        #QMessageBox.about(self,'test', str(line[0]))

                        #for x in line:

                        #QMessageBox.about(self,'test', str(x))

                        strs=str(line[0]) + '\t' + str(line[1])

                        f.write(strs+"\n")  

                f.close   

                coord_file = open(os.path.join(diretory_home, 'shp_points_coord.txt').replace('\\', '/'), 'r')

            else:

                # if txt exist

                #QMessageBox.about(self,'test', str('txt exist'))

                coord_file = open(str(input_txt), 'r')

            

            rows = [line.split('\t') for line in coord_file]

            #rows[-1] = rows[-1].strip()

            #print rows

            new_list = ['{:.3f}'.format(x) for x in list_median]

            #print new_list

            cols = list(zip(*rows))

            cols.insert(0, new_list)

            #print cols

            rows = list(zip(*cols))

            output_file.writelines(['\t'.join(row) for row in rows])

            #print cols

            #print rows

            #for linne in coord_file.readlines():

                ##print linne

                #output_file.write(str(linne) + '\t' + str(row) for row in list_median)

               

                

            #list_median.append(median)

                

            output_file.close()      

            input_file.close()

            coord_file.close()

            try:

                os.remove(file)  

                os.rename(os.path.join(os.path.dirname(inf), 'newfile.txt'), file)   

            except IndexError:

                pass 

        

        # insert headers in files with MEDIAN X and Y fields

        for file in glob.glob(str(inf) + '\*.txt'):   

            import os

            headers = ['MEDIAN', 'X', 'Y']

            tmp = open('TMP', 'w')

            orig = open(file, 'r')

            tmp.write('\t'.join(headers) + '\n')

            for line in orig.readlines():

                tmp.write(line)

            orig.close()

            tmp.close()

            os.remove(file)

            os.rename('TMP', file)    

                    

        

        # create shapefiles with the coordinates and median values

        indir = inf

        outdir = inf

        for root, dirs, files in os.walk(indir):

            for file in files:

                if file.endswith('.txt'):

                    fullname = os.path.join(root, file).replace('\\', '/')

                    #QMessageBox.about(self,'test', str(fullname))

                    filename = os.path.splitext(os.path.basename(fullname))[0]

                    #QMessageBox.about(self,'test', str(str(filename)))

                    uri = 'file:///%s?crs=%s&delimiter=%s&xField=%s&yField=%s&decimal=%s' % (fullname, 'EPSG:3763', '\t', 'x', 'y',',')

                    #QMessageBox.about(self,'test', str(outdir + '/' + filename + '.shp'))

                    layer = QgsVectorLayer(uri, 'my_layer', 'delimitedtext')

                    QgsVectorFileWriter.writeAsVectorFormat(layer, outdir + '/' + filename + '.shp', 'CP1250', None, 'ESRI Shapefile')  

                    

        

        i = 1

        for file_in in glob.glob(str(inf) + '\*.shp'): 

            txt_file = str(diretory_home) + '\color.txt'

            #QMessageBox.about(self,'test', str(os.path.splitext(os.path.basename(file_in))[0]))

            out = os.path.join(str(inf), os.path.splitext(os.path.basename(file_in))[0] + '_' + str(i) + '.sdat')

            outwithmask = os.path.join(str(inf), 'mask'+ os.path.splitext(os.path.basename(file_in))[0] + '_' + str(i) + '.sdat')

            colorraster = os.path.join(str(images_out), os.path.splitext(os.path.basename(file_in))[0] + '_' + str('%02d'%i) + '.tif')

            #output = os.path.join(str(inf), 'final' + str(i) + '.tif')

            #QMessageBox.about(self,'test', str(out))

            #QMessageBox.about(self,'test', str(extent))

            Processing.runAlgorithm("saga:ordinarykriging",None,file_in,"MEDIAN",0,True,False,100,-1,4,1,"a + b * x",0,1000,0,2,5,0,True,extent,1,0,out,None)

            #QMessageBox.about(self,'test', str('yes'))

            Processing.runAlgorithm("saga:clipgridwithpolygon", None, out, inputMask,outwithmask)

            #QMessageBox.about(self,'test', str(i))

            

            # color raster file

            #cmd1 = "gdaldem.exe"

            #subprocess.call([cmd1, "color-relief", outwithmask, txt_file, colorraster], shell=True)

            

            

            

            # second attempt to color raster file (test)

            from qgis.PyQt import QtGui

            

            #Next we load the raster layer:

            lyr = QgsRasterLayer(outwithmask, 'Temperature (Celsius)')

            

            #Now we create a generic raster shader object:

            s = QgsRasterShader()

            

            #Then we instantiate the specialized ramp shader object:

            c = QgsColorRampShader()

            

            #We must name a type for the ramp shader. In this case we use an interpolatedshader:

            c.setColorRampType(QgsColorRampShader.INTERPOLATED)

            

            #Now we ll create a list hold our color ramp definition:

            iii = []

            

            #Then we populate the list with color ramp color values corresponding to elevation value ranges:

            iii.append(QgsColorRampShader.ColorRampItem(33.2612, QtGui.QColor('#1a9641'), '33.2612'))

            

            iii.append(QgsColorRampShader.ColorRampItem(40.6101, QtGui.QColor('#a6d96a'), '40.6101'))

            

            iii.append(QgsColorRampShader.ColorRampItem(47.959, QtGui.QColor('#ffffc0'), '47.959'))

            

            iii.append(QgsColorRampShader.ColorRampItem(55.3079, QtGui.QColor('#fdae61'), '55.3079'))

            

            iii.append(QgsColorRampShader.ColorRampItem(62.6569, QtGui.QColor('#d7191c'), '62.6269'))

            

            #Now we assign the color ramp to our shader:

            c.setColorRampItemList(iii)

            

            #Now we tell the generic raster shader to use the color ramp:

            s.setRasterShaderFunction(c)

            

            #Next we create a raster renderer object with the shader:

            ps = QgsSingleBandPseudoColorRenderer(lyr.dataProvider(), 1, s)

            

            #We assign the renderer to the raster layer:

            lyr.setRenderer(ps)

            

            #Finally we add the layer to the canvas to view it:

            QgsMapLayerRegistry.instance().addMapLayer(lyr)   

      

 #___Create composition___      

            # composition

            layerset = []

            #QgsMapLayerRegistry.instance().addMapLayer(RUSLE_color_raster)

            layerset.append(lyr.id())

            

            myMap = QgsMapSettings()

        

            actual_crs = lyr.crs()

            #map_proj = QgsCoordinateReferenceSystem(3763, QgsCoordinateReferenceSystem.PostgisCrsId)

            myMap.setDestinationCrs(actual_crs)

            myMap.setLayers(layerset)

            mapRectangle = myMap.fullExtent()

            #mapRectangle.scale(0.5)

            myMap.setExtent(mapRectangle)

            myMap.setOutputSize(QSize(800,400))

          

            

            myComposition = QgsComposition(myMap)

            myComposition.setPlotStyle(QgsComposition.Print)

           

            myFile = str(diretory_home) + '/template2.qpt'

        

            

            myTemplateFile = open(myFile, 'rt')

            myTemplateContent = myTemplateFile.read()

            ###QMessageBox.about(self, "plano", myTemplateContent)

            myTemplateFile.close()    

            myDocument = QtXml.QDomDocument()

            myDocument.setContent(myTemplateContent)

            myComposition.loadFromTemplate(myDocument)  

           

            x,y = 20,20

            # paper A4

            w,h = myComposition.paperWidth(), myComposition.paperHeight()

            #QMessageBox.about(self,'test', str(w))

            #QMessageBox.about(self,'test', str(h))

            composerMap = QgsComposerMap(myComposition,x,y,w-30,h-70)

            composerMap.gridEnabled()

            myComposition.addComposerMap(composerMap)

            composerMap.setLayerSet(myMap.layers())

          

            

            #legend

            legend = QgsComposerLegend(myComposition)

            legend.setComposerMap(composerMap)

            #legend.model().setLayerSet(myMap.layers())

            #legend.model().setLayerSet

            myComposition.addComposerLegend(legend)

            legend.setItemPosition(252.2,160.1)

            legend.update()                

            #legend.setItemPosition (100,122)

            #legend.setFrameEnabled(True)

            #legend.setScale(.7)   

            

            # title

            title = QgsComposerLabel(myComposition)

            title.setText(str(os.path.splitext(os.path.basename(file_in))[0]))

            title.setFont(QFont("Cambria",40, QFont.Bold))

            title.setItemPosition(185.8,5.2)

            title.adjustSizeToText()

            #title.setFrameEnabled(True)

            myComposition.addItem(title)                

    

            # arrow picture

            pic_path = str(diretory_home) + '/north.png'

            pic = QgsComposerPicture(myComposition)

            pic.setPictureFile(pic_path)

            pic.setRotationMap(0)

            pic.setSceneRect(QRectF(0,0,20,20))

            myComposition.addComposerPicture(pic)

            pic.setItemPosition(2.2,2.6)

            #pic.setScale(2)

            #pic.setFrameEnabled(True)

            #pic.refreshPicture()

            

            # add scale bar

            item = QgsComposerScaleBar(myComposition)

            item.setComposerMap(composerMap)

            myComposition.addComposerScaleBar(item)

            item.setStyle('Double Box') # optionally modify the style

            #item.SegmentSizeMode('SegmentSizeFitWidth')

            item.adjustBoxSize()

            #item.setNumSegments(4)

            item.setUnitLabeling("km")

            #item.setNumMapUnitsPerScaleBarUnit(1)

            #item.setHeight(8)

            ##item.correctXPositionAlignment(10,10)

            item.applyDefaultSize()

            item.setItemPosition(0,190)

            item.update()

            

           

            output_jpeg = os.path.join(str(images_out), 'Temperature (Celsius)' + str('%02d'%i) + '.tif')

            myImage = myComposition.printPageAsRaster(0)

            myImage.save(output_jpeg)

#___End composition___            

            

           

       

            

            #cmd2 = "gdal_translate.exe"

            #subprocess.call([cmd2, "-ot", "Byte", "-of", "GIF", "-scale", colorraster, output], shell=True)

            i = i+1

        times = i

        QMessageBox.about(self,'test', str(times))

    

        #nFrames = len([item for item in os.listdir(directory) if os.path.isfile(os.path.join(directory, item))])

        nFrames = times

        os.chdir('C:/ffmpeg/bin')

        

       

        subprocess.call(['ffmpeg.exe', '-f', 'image2', '-r','1/2','-i',os.path.join(str(images_out), 'Temperature (Celsius)%02d.tif'), '-vcodec', 'libx264',os.path.join(str(images_out), 'movie.mp4')], shell=True)

            #a = subprocess.call(['ffmpeg.exe', '-version'])

        QMessageBox.about(self,'Created', str('The video was created. Please see in the folder. Click on Play button to display the video.'))

            

        

            

       