import numpy as np
import os
from glob import glob
from pathlib import Path
from multiprocessing import Pool
import hashlib # md5
import time
import shutil
import datetime
########################################################
# 函数区
########################################################

spacetySliceEnvPathExEPath=r"d:\ProgramData\anaconda3\envs\spacetySliceEnv\python.exe"

logPath=r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\log\process.log"


def existOrCreate(dirpath):
    if not os.path.exists(dirpath):
        os.makedirs(dirpath)

def moveDir(srcfolderPath,targetfolderPath):
    try:
        shutil.copytree(srcfolderPath,targetfolderPath,dirs_exist_ok=True)
        print("sucess: copy  ", srcfolderPath,targetfolderPath )
        shutil.rmtree(srcfolderPath)
        print("sucess: rmtree  ", srcfolderPath)
        return "sucess: copy and rmtree   from {} to  {}".format(srcfolderPath,targetfolderPath),True
    except Exception as e:
        print("failed: copy  ", srcfolderPath, targetfolderPath)
        return "failed: copy and rmtree   from {} to  {}".format(srcfolderPath,targetfolderPath),False

def writeoutlog(logPath,cmdtxt):
    with open(logPath,'a',encoding="utf-8") as f:
        f.write(cmdtxt)
        f.write("\n")


def preProcessTiFF(tiffpath,txtpath,preFolderPath):

    file_path = Path(tiffpath)
    directory_path = str(file_path.resolve().parent.name)
    rootname=Path(tiffpath).stem
    outpngpath=os.path.join(preFolderPath,"I"+directory_path+"_"+rootname+'.png')
    programpath=r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\generatorRasterSlicesTools\SpacetyTIFFDataStretch2PNG_AC.py"

    # cmdtxt=r"{} {} -i {} -o {} --filemode --SquareRoot".format(spacetySliceEnvPathExEPath,programpath,tiffpath,outpngpath)
    cmdtxt=r"{} {} -i {} -p {} -o {} --slicemode --SquareRoot".format(
        spacetySliceEnvPathExEPath,programpath,
        tiffpath,
        txtpath,
        preFolderPath
    ) # 直接切片
    if os.system(cmdtxt) ==2:
        print("sucess:",cmdtxt)
        writeoutlog(logPath, "sucess:  {}\n".format(cmdtxt))
        return 2
    else:
        print("failed:",cmdtxt)
        writeoutlog(logPath, "failed:  {}\n".format(cmdtxt))
        return 3


def processMJPort(srcFolderPath,outMJPortSumTxtPath,outMJPortFolderPath,MLCShapeFilePath,JLCShapeFilePath,MJLCShapeFilePath):
    programpath = r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\generatorRasterSlicesTools\SplitShipPortRasterTools_AC.py"
    cmdtxt=r"{} {} -s {} -o {} -f {} -m {} -j {} -jm {}".format(
        spacetySliceEnvPathExEPath,programpath,
        srcFolderPath,outMJPortSumTxtPath,outMJPortFolderPath,
        MLCShapeFilePath, JLCShapeFilePath, MJLCShapeFilePath
    )
    if os.system(cmdtxt) ==2:
        print("sucess:",cmdtxt)
        writeoutlog(logPath, "sucess:  {}\n".format(cmdtxt))
        # return "sucess:  {}".format(cmdtxt)
        return 2
    else:
        print("failed:",cmdtxt)
        writeoutlog(logPath, "failed:  {}\n".format(cmdtxt))
        # return "failed:  {}".format(cmdtxt)
        return 3


def preProcessShipPortTools(srcFolderPath,targetFolderPath,outTargetFolderPath):
    writeoutlog(logPath, "====================================================================================\n")
    writeoutlog(logPath, "time: {}".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
    writeoutlog(logPath, "srcFolderPath: {}".format(srcFolderPath))
    writeoutlog(logPath, "targetFolderPath: {}".format(targetFolderPath))
    writeoutlog(logPath, "process start \n")
    #
    # # 文件接口创建
    # MPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","民港口")
    # JPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","军港口")
    # MJPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","混合港口")
    # NoPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","无港口")
    #
    # existOrCreate(MPortFolder)
    # existOrCreate(JPortFolder)
    # existOrCreate(MJPortFolder)
    # existOrCreate(NoPortFolder)

    outMJPortSumTxtPath=os.path.join(targetFolderPath,"AC-图像预处理","sumMJPort.txt")
    outMJPortFolderPath=os.path.join(targetFolderPath,"AC-图像预处理","sumMJPortFolder")

    existOrCreate(outMJPortFolderPath)

    MLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\港口（民船）.shp"
    JLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\军港.shp"
    MJLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\军民一体港口.shp"

    processMJPortflag=processMJPort(srcFolderPath,outMJPortSumTxtPath,outMJPortFolderPath,MLCShapeFilePath,JLCShapeFilePath,MJLCShapeFilePath)

    writeoutlog(logPath, "\nprocess finished \n")
    # moverTip,moveflag=moveDir(targetFolderPath, outTargetFolderPath)
    # writeoutlog(logPath, moverTip)
    writeoutlog(logPath, "====================================================================================\n")
    # if moveflag:
    #     pass
    # else:
    #     exit(3)


if __name__ == '__main__':

    # 20250813-不分类 条带模式
    srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250813-不分类\0-原图"
    preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250813-不分类\A-预处理\AB-图像预处理"
    targetfolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250813-不分类\A-预处理\AB-图像预处理"
    preProcessShipPortTools(srcFolderPath, preFolderPath, targetfolderPath)
    #
    # 20250818 条带模式
    srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250818-不分类\0-原图"
    preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250818-不分类\A-预处理\AB-图像预处理"
    targetfolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250818-不分类\A-预处理\AB-图像预处理"
    preProcessShipPortTools(srcFolderPath, preFolderPath,targetfolderPath)
    #
    # 20250826-不分类 条带模式
    srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\0-原图"
    preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\A-预处理"
    targetfolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\A-预处理"
    preProcessShipPortTools(srcFolderPath, preFolderPath,targetfolderPath)
    #
    # 20250903-不分类条带模式
    srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类\0-原图"
    preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类\A-预处理"
    targetfolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类\A-预处理"
    preProcessShipPortTools(srcFolderPath, preFolderPath,targetfolderPath)
    #
    # 20250910-不分类 条带模式
    srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250910-不分类\0-原图"
    preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250910-不分类\A-预处理"
    targetfolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250910-不分类\A-预处理"
    preProcessShipPortTools(srcFolderPath, preFolderPath,targetfolderPath)










