import os
import shutil
import UtilPack as util
import PoleXLS as myxl
import VWorldAPIs as vwapi


ImgDirPath = util.NormalizePath("/Volumes/ExSSD/Working/Images")
ImgSortedPath = util.NormalizePath("/Volumes/ExSSD/Working/Sorted")
XlsDirPath = util.NormalizePath("/Users/minarinari/Workspace/Python/UtilityPole_Info")

indexFile = ".index.maps"

# 엑셀을 연다.
XLSPath = os.path.join(XlsDirPath, "EPoleDB.xlsx")
tempxls = myxl.PoleXLS(XLSPath) 
tempxls.DBXLSOpen()

TrgSheet = "ImgInfo"
TrgSheetLastCol = 8
# index, sheet, date, level, relpath, area, juso, gps
nLastRow = tempxls.FindLastRow(TrgSheet)

for nRow in range(1, nLastRow):
    listValues = tempxls.GetAllRowValue(TrgSheet, nRow, TrgSheetLastCol )

    if TrgSheetLastCol != len(listValues):
        continue

    #print(listValues)

    level = listValues[3]
    relpath = listValues[4]
    area = listValues[5]
    gps = [7]

    if None == area or True == util.IsEmptyStr(area):
        continue

    pathSrcFull = os.path.join(ImgDirPath, relpath)
    if False == os.path.exists(pathSrcFull):
        continue
    
    tempPath = f"{area}_{relpath}"
    pathTrgFull = os.path.join(ImgSortedPath, tempPath)
    pathTrgDir = os.path.dirname(pathTrgFull)

    if False == os.path.exists(pathTrgDir):
        os.makedirs(pathTrgDir)

        indexfilePath = os.path.join(pathTrgDir, indexFile)

        with open(indexfilePath, "w") as file:
            file.write(f"{area} : {level} ")
        

    print(f"{pathSrcFull} -> {pathTrgFull}")
    shutil.copy(pathSrcFull, pathTrgFull)




    


#a = "/Volumes/ExSSD/Working/Images"
#b = "/Volumes/ExSSD/Working/Images/a"

#c = os.path.relpath(b, a)
#d = os.path.realpath(c)

# 37.53245379972222,126.96456829972223

#juso, area = vwapi.GetJusofromGPS(37.533949916666664,126.99432473611111)

#print(f"{area} : {juso}")

#Pole : 37.54353059972222,126.9650134
#Trg : 37.5439572,126.95750569972222

#srcLat = 37.54353059972222
#srcLon = 126.9650134

#trgLat = 37.5439572
#trgLon = 126.95750569972222

#dist = util.GetDistanceGPS(srcLat, srcLon, trgLat, trgLon)
#print(dist)

