import os
import shutil
import pandas as pd
from pathlib import Path
from b2btool.blob.storage import *


try:
    conversion = pd.read_excel(
        r"J:\PTCR\Users\RECS\.b2btool\drawings_to_jde.xlsx",
        dtype={"ID": str, "JDE": str},
    )
except FileNotFoundError as ex:
    print(ex.args)



def find_jde(drawing_name, conversion):
    line = conversion[conversion["drawing_number"] == drawing_name]
    return line.JDE.tolist()  # list of jde equivalent.



def copy_file(image, image_renamed, push=False):
    if os.path.exists(image_renamed):
        shutil.move(image_renamed, f"duplicate/{image_renamed}")
        print(f"[!]: {image_renamed} -> duplicate")
    else:
        shutil.copyfile(image, image_renamed)
        print(f"[Copy] {image} \t->\t {image_renamed}")
        if push:
            push_to_blob_unique_image(image_renamed)


def rename_to_drawing_number(image_jpg, push):
    """Rename drawings-name >>> jde-name
    """
    image_name = Path(image_jpg).stem
    # example of renaming pictures names: PT0019300.jpg -> 340247.jpg
    # create a folder for duplicate jde pictures.
    os.makedirs("duplicate", exist_ok=True)
    if (
        os.path.exists(image_jpg) and image_name in conversion["drawing_number"].tolist()
    ):
        jde_equivalences = find_jde(image_name, conversion)

        if len(jde_equivalences) == 1:
            # single jde for one drawing
            jde_equivalent_jpg = f"{jde_equivalences[0]}.jpg"
            copy_file(image_jpg, jde_equivalent_jpg,  push)

        elif len(jde_equivalences) > 1:
            # Multiple jde's for one drawing
            for jde in jde_equivalences:
                jde_jpg = f"{jde}.jpg"
                copy_file(image_jpg, jde_jpg, push)
            os.remove(image_jpg)
            print(f"[Deleted]: {image_jpg}")

        else:
            # for empty list simply ignore.
            pass

    else:
        # it's a jde part image.
        if push:
            push_to_blob_unique_image(image_jpg)
        pass
import os
import shutil
import pandas as pd
from pathlib import Path
from b2btool.blob.storage import *


try:
    conversion = pd.read_excel(
        r"J:\PTCR\Users\RECS\.b2btool\drawings_to_jde.xlsx",
        dtype={"ID": str, "JDE": str},
    )
except FileNotFoundError as ex:
    print(ex.args)



def find_jde(drawing_name, conversion):
    line = conversion[conversion["drawing_number"] == drawing_name]
    return line.JDE.tolist()  # list of jde equivalent.



def copy_file(image, image_renamed, push=False):
    if os.path.exists(image_renamed):
        shutil.move(image_renamed, f"duplicate/{image_renamed}")
        print(f"[!]: {image_renamed} -> duplicate")
    else:
        shutil.copyfile(image, image_renamed)
        print(f"[Copy] {image} \t->\t {image_renamed}")
        if push:
            push_to_blob_unique_image(image_renamed)


def rename_to_drawing_number(image_jpg, push):
    """Rename drawings-name >>> jde-name
    """
    image_name = Path(image_jpg).stem
    # example of renaming pictures names: PT0019300.jpg -> 340247.jpg
    # create a folder for duplicate jde pictures.
    os.makedirs("duplicate", exist_ok=True)
    if (
        os.path.exists(image_jpg) and image_name in conversion["drawing_number"].tolist()
    ):
        jde_equivalences = find_jde(image_name, conversion)

        if len(jde_equivalences) == 1:
            # single jde for one drawing
            jde_equivalent_jpg = f"{jde_equivalences[0]}.jpg"
            copy_file(image_jpg, jde_equivalent_jpg,  push)

        elif len(jde_equivalences) > 1:
            # Multiple jde's for one drawing
            for jde in jde_equivalences:
                jde_jpg = f"{jde}.jpg"
                copy_file(image_jpg, jde_jpg, push)
            os.remove(image_jpg)
            print(f"[Deleted]: {image_jpg}")

        else:
            # for empty list simply ignore.
            pass

    else:
        # it's a jde part image.
        if push:
            push_to_blob_unique_image(image_jpg)
        pass
# list of jde equivalent. Rename drawings-name >>> jde-name # example of renaming pictures names: PT0019300.jpg -> 340247.jpg # create a folder for duplicate jde pictures. # single jde for one drawing # Multiple jde's for one drawing # for empty list simply ignore. # it's a jde part image.