import os
from posixpath import ismount
import sys
import time
import subprocess
import datetime
import shutil
import json
import getopt
from ffmpeg_progress_yield import FfmpegProgress
from tqdm import tqdm
import filedate
def d_print(msg):
    if _TEST:
        print(msg)

if sys.platform == "linux" or sys.platform == "linux2":
    # linux
    ops =  "linux"
elif sys.platform == "darwin":
    # OS X
    ops =  "osx"
elif sys.platform == "win32":
    ops =  "win"
try:
    opts, args = getopt.getopt(sys.argv[1:], "q:x:ta:r:fp:d:s:", ["command=", "help", "output=", "backup"])
except getopt.GetoptError as err:
    # print help information and exit:
    print(str(err))  # will print something like "option -a not recognized"
    #usage()
    sys.exit(2)
output = None
# QJ : getopts
_DEV = "gopro9_1"
_SEQUENCES = []
_FLIP = False
_TEST = False
_QUALITY = ["35","35","25"]
if ops == "win":
    _SOURCE_DIR = "p:\\imported\\"
else:
    _SOURCE_DIR = "/share/Photo/imported/"
for o, a in opts:
    if o == "-h":
        _ACTION = True
    elif o in ("-d", "--device"):
        _DEV = True
    elif o in ("-q", "--quality"):
        _QUALITY = a.split(",")
    elif o in ("-p", "--part"):
        _DAY_DIRS = a.split(",")
    elif o in ("-f", "--flip"):
        _FLIP = True
    elif o in ("-r", "--source"):
        _SOURCE_DIR = a
    elif o in ("-a", "--action"):
        _ACTION = a
    elif o in ("-t", "--test"):
        _TEST = True
    elif o in ("-x", "--file"):
        _FILE = a
    elif o in ("-s", "--sequence"):
        a = a.split(",")
        for s in a:
            print(s)
            _SEQUENCES.append("{:04d}".format(int(s)))
            

    else:
        _WIZZARD = True
        
#_SOURCE_DIR = "/media/nas/nas-photo/imported/"





#print(_FULL_PATH)
if _ACTION == "flip":
    for _DAY_DIR in _DAY_DIRS:
        if ops == "win":
            _FULL_PATH = _SOURCE_DIR + "\\" + _DEV + "\\" + _DAY_DIR + "\\"
        else:
            _FULL_PATH = _SOURCE_DIR + "/" + _DEV + "/" + _DAY_DIR + "/"
        export_name = "X" + _FILE
        if ops == "win":
            myCmd =  [
        "ffmpeg", "-i", _FILE ,"-c", "copy", "-metadata:s:v:0", "rotate=180", "-y", export_name
    ]
        else:
            myCmd =  [
        "/opt/ffmpeg/ffmpeg", "-f", "-i", _FILE, "-c", "copy", "-metadata:s:v:0", "rotate=180", "-y", export_name
    ]   
        Path = "~/Documents/File.txt"

        # filedate.File(Path).set(
        #     created = "1st February 2003, 12:30",
        #     modified = "3:00 PM, 04 May 2009",
        #     accessed = "08/07/2014 18:30:45"
        # )
        create_date = filedate.File(_FULL_PATH + _FILE).get()
        if _TEST:
            print(_FULL_PATH)
            print(myCmd)
        os.chdir(_FULL_PATH)
        proc = subprocess.Popen(myCmd,stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)
        print(f"Fliping {_FILE} to {export_name} in {_DAY_DIR}...".ljust(100))
        ff = FfmpegProgress(myCmd)
        with tqdm(total=100, position=1, desc="Fliping") as pbar:
            for progress in ff.run_command_with_progress():
                pbar.update(progress - pbar.n)
            pbar.close()

        print(f"Done".ljust(130),end='', flush=True)
        # get the output
        #print(ff.stderr)                      
        #ffmpeg  -i .\GH010149.MP4  -c:v hevc_amf -rc cqp -qp_i 42 -qp_p 42 -quality quality -crf 25 output7.mp4
        try:
            shutil.move(_FILE, "__" + export_name)
            shutil.move(export_name, _FILE)
            
            filedate.File(_FULL_PATH + _FILE).set(
                created = create_date["created"],
                modified = create_date["modified"],
                accessed = create_date["accessed"]
            )
        except ValueError as e:
            print(e)
            print(f"Failed to move {export_name}")
if _ACTION == "join":
    for _DAY_DIR in _DAY_DIRS:
        if ops == "win":
            _FULL_PATH = _SOURCE_DIR + "\\" + _DEV + "\\" + _DAY_DIR + "\\"
        else:
            _FULL_PATH = _SOURCE_DIR + _DEV + "/" + _DAY_DIR + "/"
        video_seq = {}
        
        if os.path.exists(_FULL_PATH):
            if os.path.exists(_FULL_PATH + "100GOPRO"):
                
                if ops == "win":
                    _FULL_PATH = _FULL_PATH + "100GOPRO\\"
                else:
                    _FULL_PATH = _FULL_PATH + "100GOPRO/"
            for filename in os.listdir(_FULL_PATH):
                
                if filename.endswith(".MP4") and not filename.startswith("joined_"):
                    prefix = filename[0:2]
                    part = prefix + filename[2:4]
                    seq = filename[4:8]
                    if not seq in video_seq:
                        video_seq[seq] = []
                    video_seq[seq].append(part + seq + ".MP4")
                    # print(filename)
                    # print(seq)
                    # print(part)
            # print(json.dumps(video_seq,indent=2))
            _PROCESSED = _FULL_PATH + "processed"
            if not os.path.exists(_PROCESSED):
                os.mkdir(_PROCESSED)
            # print(f"SEQUENCES : {_SEQUENCES}")
            os.chdir(_FULL_PATH)
            for vid in video_seq:
                
                if len(_SEQUENCES) != 0 and vid not in _SEQUENCES:
                    continue
                export_name = "joining_"  + vid + ".MP4"
                parts = video_seq[vid]
                
                parts.sort()
                # print(parts)
                if len(parts) == 1:
                    shutil.move(parts[0], export_name.replace("joining","joined"))
                    try:
                        shutil.move(parts[0].replace("MP4", "THM"), _PROCESSED)
                    except:
                        print(f"Failed to move {parts[0].replace("MP4", "THM")}")
                        
                    try:
                        shutil.move(parts[0].replace("MP4", "LRV").replace("GH","GL"), _PROCESSED)
                    except:
                        print(f"Failed to move {parts[0].replace("MP4", "LRV")}")
                    continue
                # print(type(parts))
                #ffmpeg -i concat:"input1.mp4|input2.mp4" output.mp4
                
                
                f = open("video.txt", "w")
                f.write("# Files to join\n")
                f.close()
                f = open("video.txt", "a")
                final_size = 0
                create_date = ""
                for p in parts:
                    if create_date == "":
                        create_date = filedate.File(_FULL_PATH + p).get()
                    f.write(f"file '{p}'\n")
                    file_stats = os.stat(p)
                    final_size += file_stats.st_size
                f.close()   
                #print(final_size)
                #print(_FULL_PATH)

                if _TEST:
                    input(" ???? ")

                # print(os.getcwd())
                #status, output = subprocess.getstatusoutput(myCmd)
                
                if _FLIP:
                    if ops == "win":
                        myCmd =  [
                    "ffmpeg", "-f", "concat", "-i", "video.txt", "-c", "copy", "-metadata:s:v:0", "rotate=180","-y", export_name
                ]
                    else:
                        myCmd =  [
                    "/opt/ffmpeg/ffmpeg", "-f", "concat", "-i", "video.txt", "-c", "copy", "-metadata:s:v:0", "rotate=180", "-y", export_name
                ]
                else:
                    if ops == "win":
                        myCmd =  [
                    "ffmpeg", "-f", "concat", "-i", "video.txt","-vcodec", "copy", "-acodec", "copy", "-y", export_name
                ]
                    else:
                        myCmd =  [
                    "/opt/ffmpeg/ffmpeg", "-f", "concat", "-i", "video.txt","-c", "copy", "-y", export_name
                ]
           
                d_print(" ".join(myCmd))
                d_print(_FULL_PATH)
                print(f"Joining {len(parts)} files to {export_name} in {_DAY_DIR}...".ljust(100))
                try:                                    
                    os.remove(export_name) 
                except:
                    pass
                os.chdir(_FULL_PATH)
                print(_FULL_PATH)
                print(os.getcwd())
                proc = subprocess.Popen(myCmd,stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)
                #proc = subprocess.Popen(myCmd,stderr=subprocess.STDOUT)
                # with tqdm(total=100, position=1, desc="Convert") as pbar:
                #     for progress in ff.run_command_with_progress():
                #         pbar.update(progress - pbar.n)
                
                cur_stat = 0
                d_print("Running commad2")
                with tqdm(total=100, position=1, desc="Join") as pbar:
                    position=0
                    while proc.returncode is None:
                        
                        #pbar.update()
                        try:
                            file_stats = os.stat(export_name)
                            cur_size = file_stats.st_size 
                            final_size
                            cur_stat = (cur_size / final_size) * 100
                            
                            pbar.update(int(cur_stat) - position)
                            position = int(cur_stat)
                            proc.poll()
                        except:
                            pass
                        
                        time.sleep(3)
                
                    pbar.update(1)
                    #pbar.close()
                print("\n")
                file_stats = os.stat(export_name)
                print(f"Exported size: {str(file_stats.st_size )}")
                print(f"Expected size: {final_size}")
                if cur_stat < 99:
                    print("Something is wrong")
                if final_size + 1000000 < final_size:
                    print("Something is wrong")
                else:
                    print("Size OK")
                try:
                    shutil.move(export_name, export_name.replace("joining","joined"))
                    filedate.File(_FULL_PATH + export_name.replace("joining","joined")).set(
                        created = create_date["created"],
                        modified = create_date["modified"],
                        accessed = create_date["accessed"]
                    )
                except:
                    print(f"Failed to move {export_name}")
                print("\r\n",flush=True)
                print(f"\rProcess finished with rc: {proc.returncode}",flush=True)                    
                # get the output
                status = 0
                
                if status == 0 and not _TEST:
                    print("Moving...")
                    for p in parts:
                        try:
                            shutil.move(p, _PROCESSED)
                        except:
                            print(f"Failed to move {p}")
                        try:
                            shutil.move(p.replace("MP4", "THM"), _PROCESSED)
                        except:
                            print(f"Failed to move {p.replace("MP4", "THM")}")
                            
                        try:
                            shutil.move(p.replace("MP4", "LRV").replace("GH","GL"), _PROCESSED)
                        except:
                            print(f"Failed to move {p.replace("MP4", "LRV")}")
            try:                                    
                os.remove("video.txt") 
            except:
                pass
        
if _ACTION == "encode":
    print("Encode Job")
    for _DAY_DIR in _DAY_DIRS:
        if ops == "win":
            _FULL_PATH = _SOURCE_DIR + "\\" + _DEV + "\\" + _DAY_DIR + "\\"
        else:
            _FULL_PATH = _SOURCE_DIR + "/" + _DEV + "/" + _DAY_DIR + "/"
        if os.path.exists(_FULL_PATH):
            if os.path.exists(_FULL_PATH + "100GOPRO"):
                _FULL_PATH = _FULL_PATH + "100GOPRO"
            parts = []
            for filename in os.listdir(_FULL_PATH):
                if filename.startswith("joined_") and filename.endswith(".MP4"):
                    parts.append(filename)
        else:
            sys.exit()
                
        os.chdir(_FULL_PATH)      
        create_date = ""
        for p in parts:
            if create_date == "":
                create_date = filedate.File(_FULL_PATH + p).get()
            export_name = "encoding_" + p
            if _FLIP:
                if ops == "win":
                    myCmd =  [
                "ffmpeg", "-i", p, "-c:v", "hevc_amf", "-rc", "cqp", "-qp_i", _QUALITY[0], "-qp_p", _QUALITY[1], "-quality", "quality", "-crf", _QUALITY[2], "-metadata:s:v:0", "rotate=180", "-y", export_name, "-f", "null", "/dev/null",
            ]
                else:
                    myCmd =  [
                "/opt/ffmpeg/ffmpeg", "-display_rotation", "180",  "-i", p, "-c:v", "hevc_amf", "-rc", "cqp", "-qp_i", _QUALITY[0], "-qp_p", _QUALITY[1], "-quality", "quality", "-crf", _QUALITY[2], "-y", export_name, "-f", "null", "/dev/null",
            ]
            else:
                if ops == "win":
                    myCmd =  [
                "ffmpeg", "-i", p, "-c:v", "hevc_amf", "-rc", "cqp", "-qp_i", _QUALITY[0], "-qp_p", _QUALITY[1], "-quality", "quality", "-crf", _QUALITY[2], "-y", export_name , "-f", "null", "/dev/null",
            ]
                else:
                    myCmd =  [
                "/opt/ffmpeg/ffmpeg", "-i", p, "-c:v", "hevc_amf", "-rc", "cqp", "-qp_i", _QUALITY[0], "-qp_p", _QUALITY[1], "-quality", "quality", "-crf", _QUALITY[2], "-y", export_name , "-f", "null", "/dev/null",
            ]
            #print(myCmd)
            d_print(" ".join(myCmd))
            print(f"Encoding {p} to {export_name} in {_DAY_DIR}...".ljust(100))
            ff = FfmpegProgress(myCmd)
            with tqdm(total=100, position=1, desc="Encode") as pbar:
                for progress in ff.run_command_with_progress():
                    pbar.update(progress - pbar.n)
                pbar.close()
            # get the output
            #print(ff.stderr)                      
            #ffmpeg  -i .\GH010149.MP4  -c:v hevc_amf -rc cqp -qp_i 42 -qp_p 42 -quality quality -crf 25 output7.mp4
            try:
                shutil.move(export_name, export_name.replace("encoding","encoded"))
                filedate.File(_FULL_PATH + export_name.replace("encoding","encoded")).set(
                        created = create_date["created"],
                        modified = create_date["modified"],
                        accessed = create_date["accessed"]
                    )
            except:
                print(f"Failed to move {export_name}")
            
        