# -------------------------------------------------------------------
# ORTHOGRAPHIC
# Your personal aerial satellite. Always on. At any altitude.*
# Developed by MarStrMind
# License: Open Software License 3.0
# Up to date version always on marstr.online
# -------------------------------------------------------------------
# tileinfo.py
# Stores and retrieves information about which material borders on
# which edge, aiding in the choice of material to generate seamless
# images.
# -------------------------------------------------------------------

from defines import *
from functions import *
from log import *


class mstr_tileinfo:

    def __init__(self, lat, lng, latnum, lngnum, latlngfld):
        self._lat = lat
        self._lng = lng
        self._latnum = latnum
        self._lngnum = lngnum
        self._latlngfld = latlngfld
        self._adjfile = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/" + str(self._latnum) + "_" + str(self._lngnum)
        #self._adjfile = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/adjinfo"
        self._cplfile = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/cplinfo"
        self.createDataFile()
        self.createCompletionFile()


    def createDataFile(self):
        if os.path.isfile(self._adjfile) == False:
            open(self._adjfile, 'a').close()


    def createCompletionFile(self):
        if os.path.isfile(self._cplfile) == False:
            open(self._cplfile, 'a').close()


    def add_adjacency_data(self, tv, th, tag, value, source, adj):
        line = ""
        line = line + str(tv) + " "
        line = line + str(th) + " "
        line = line + tag + " "
        line = line + value + " "
        line = line + str(source) + " "
        line = line + str(adj) + "\n"

        with open(self._adjfile, 'a') as textfile:
            textfile.write(line)


    def add_completion_data(self, tv, th, tag, value, source, adj):
        line = ""
        line = line + str(tv) + " "
        line = line + str(th) + " "
        line = line + tag + " "
        line = line + value + " "
        line = line + str(source) + " "
        line = line + str(adj) + "\n"

        with open(self._cplfile, 'a') as textfile:
            textfile.write(line)


    def get_adjacency_for_tag_and_value(self, tv, th, tag, value):
        adj = []
        fnlines = []
        fn = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/" + str(tv) + "_" + str(th)
        if os.path.isfile(fn) == True: # It is possible that the requested file does not yet exist
            with open(fn) as textfile:
                fnlines = textfile.readlines()

            for ln in fnlines:
                l = ln.split(" ")
                if int(l[0]) == tv and int(l[1]) == th:
                    if l[2] == tag and l[3] == value:
                        l[5] = l[5].replace("\n", "")
                        adj.append(int(l[4]))
                        adj.append(l[5])
                        break

        return adj


    def get_adjacency_for_tag_and_value_in_lat_lng(self, lat, lng, tag, value, tv, th, brd):
        latlngfld = self.latlng_folder([lat, lng])
        adj = []
        fn = mstr_datafolder + "z_orthographic/data/" + latlngfld + "/" + str(tv) + "_" + str(th)
        if os.path.isfile(fn) == True:
            fnlines = []
            with open(fn) as textfile:
                fnlines = textfile.readlines()

            if brd == "b":
                # Find highest tile number
                top = 0
                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[0]) > top:
                        top = int(l[0])

                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[0]) == top and int(l[1]) == th:
                        if l[2] == tag and l[3] == value:
                            adj.append(int(l[4]))
                            adj.append(l[5])

            if brd == "l":
                # Find highest tile number
                right = 0
                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[1]) > right:
                        right = int(l[1])

                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[1]) == right and int(l[0]) == tv:
                        if l[2] == tag and l[3] == value:
                            adj.append(int(l[4]))
                            adj.append(l[5])

            if brd == "r":
                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[0]) == 1 and int(l[1]) == tv:
                        if l[2] == tag and l[3] == value:
                            adj.append(int(l[4]))
                            adj.append(l[5])

            if brd == "t":
                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[1]) == 1 and int(l[0]) == th:
                        if l[2] == tag and l[3] == value:
                            adj.append(int(l[4]))
                            adj.append(l[5])

        return adj


    def get_completion_in_lat_lng(self, lat, lng, tv, th, brd):
        latlngfld = self.latlng_folder([lat, lng])
        adj = []
        fn = mstr_datafolder + "z_orthographic/data/" + latlngfld + "/cplinfo"
        if os.path.isfile(fn) == True:
            fnlines = []
            with open(fn) as textfile:
                fnlines = textfile.readlines()

            if brd == "b":
                # Find highest tile number
                top = 0
                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[0]) > top:
                        top = int(l[0])

                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[0]) == top and int(l[1]) == th:
                        adj.append(l[2])
                        adj.append(l[3])
                        adj.append(int(l[4]))
                        adj.append(l[5])

            if brd == "l":
                # Find highest tile number
                right = 0
                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[1]) > right:
                        right = int(l[1])

                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[1]) == right and int(l[0]) == tv:
                        adj.append(l[2])
                        adj.append(l[3])
                        adj.append(int(l[4]))
                        adj.append(l[5])

            if brd == "r":
                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[0]) == 1 and int(l[1]) == tv:
                        adj.append(l[2])
                        adj.append(l[3])
                        adj.append(int(l[4]))
                        adj.append(l[5])

            if brd == "t":
                for ln in fnlines:
                    l = ln.split(" ")
                    if int(l[1]) == 1 and int(l[0]) == th:
                        adj.append(l[2])
                        adj.append(l[3])
                        adj.append(int(l[4]))
                        adj.append(l[5])

        return adj



    def get_adjacency_for_completion(self, tv, th):
        adj = []
        fnlines = []
        with open(self._cplfile) as textfile:
            fnlines = textfile.readlines()

        for ln in fnlines:
            l = ln.split(" ")
            if int(l[0]) == tv and int(l[1]) == th:
                l[5] = l[5].replace("\n", "")
                adj.append(l[2])
                adj.append(l[3])
                adj.append(int(l[4]))
                adj.append(l[5])
                break

        return adj
    

    # Construct a folder name for latitude and longitude
    def latlng_folder(self, numbers):
        fstr = ""
        if numbers[0] >= 0: fstr = "+"
        if numbers[0] <  0: fstr = "-"
        if abs(numbers[0]) < 10: fstr = fstr + "0" + str(numbers[0])
        if abs(numbers[0]) >= 10 and numbers[0] <= 90: fstr = fstr + str(numbers[0])

        if numbers[1] >= 0: fstr = fstr + "+"
        if numbers[1] <  0: fstr = fstr + "-"
        if abs(numbers[1]) < 10: fstr = fstr + "00" + str(numbers[1])
        if abs(numbers[1]) >= 10 and numbers[0] <= 99: fstr = fstr + "0" + str(numbers[1])
        if abs(numbers[1]) >= 100 : fstr = fstr + str(numbers[1])

        return fstr