import os
from pathlib import Path

from pywintypes import com_error

from pycatia import catia as pia
from tests.create_source_parts import get_cat_part_measurable

test_files = Path("tests/cat_files")

source_cat_product = Path(os.getcwd(), test_files, "product_top.CATProduct")
source_cat_sub_1 = Path(os.getcwd(), test_files, "product_sub_1.CATProduct")
source_cat_sub_2 = Path(os.getcwd(), test_files, "product_sub_2.CATProduct")


def create_cat_products(file_name_top, file_name_sub_1, file_name_sub_2):
    documents = pia.documents

    # ####################### #
    # close all the documents #
    # ####################### #

    try:
        for document in documents.items():
            document.close()
    except com_error:
        # all documents must be closed.
        pass

    # ############################ #
    # Create the top level product #
    # ############################ #

    documents.add("Product")
    doc_root_prod = pia.active_document
    doc_root_prod.save_as(file_name_top)
    product_top = doc_root_prod.product()
    product_top.part_number = "cat_product_1"
    product_top.revision = "A.1"
    product_top.nomenclature = "pycatia product for testing"
    product_top.definition = "pycatia product for testing"

    products = product_top.products

    # ######################## #
    # Create the sub product 1 #
    # ######################## #

    documents.add("Product")
    doc_sub_1 = pia.active_document
    doc_sub_1.save_as(file_name_sub_1)
    product_sub_1 = doc_sub_1.product()
    product_sub_1.part_number = "cat_product_sub_1"
    product_sub_1.revision = "A.1"
    product_sub_1.nomenclature = "pycatia product for testing"

    products_sub_1 = product_sub_1.products
    cat_part_measurable = get_cat_part_measurable()
    documents.open(cat_part_measurable)
    doc_cat_part = pia.active_document
    products_sub_1.add_component(doc_cat_part.product())

    doc_sub_1.save()

    # ######################## #
    # Create the sub product 2 #
    # ######################## #

    documents.add("Product")
    doc_sub_2 = pia.active_document
    doc_sub_2.save_as(file_name_sub_2)
    product_sub_2 = doc_sub_2.product()
    product_sub_2.part_number = "cat_product_sub_2"
    product_sub_2.revision = "A.1"
    product_sub_2.nomenclature = "pycatia product for testing"

    product_sub_2 = product_sub_2.products
    product_sub_2.add_component(doc_cat_part.product())

    doc_sub_2.save()

    # ########################### #
    # Add the sub products to top #
    # ########################### #

    products.add_component(product_sub_1)
    products.add_component(product_sub_1)
    products.add_component(product_sub_2)

    # ################# #
    # Add new component #
    # ################# #

    # yes, the naming seems to arse backwards. ask Dassault why, not me. :-)
    products.add_new_product("ComponentReference")

    # ##################################################################### #
    # Add description instance text to the first product in the top product #
    # ##################################################################### #

    first_product = products.item(1)
    first_product.description_instance = "description instance text"

    doc_root_prod.save()

    # ####################### #
    # close all the documents #
    # ####################### #

    for document in documents.items():
        document.close()


def get_cat_product_top():
    if not source_cat_product.exists():
        pia.logger.info(f"Creating {source_cat_product}")
        create_cat_products(source_cat_product, source_cat_sub_1, source_cat_sub_2)
    return source_cat_product

import os
from pathlib import Path

from pywintypes import com_error

from pycatia import catia as pia
from tests.create_source_parts import get_cat_part_measurable

test_files = Path("tests/cat_files")

source_cat_product = Path(os.getcwd(), test_files, "product_top.CATProduct")
source_cat_sub_1 = Path(os.getcwd(), test_files, "product_sub_1.CATProduct")
source_cat_sub_2 = Path(os.getcwd(), test_files, "product_sub_2.CATProduct")


def create_cat_products(file_name_top, file_name_sub_1, file_name_sub_2):
    documents = pia.documents

    # ####################### #
    # close all the documents #
    # ####################### #

    try:
        for document in documents.items():
            document.close()
    except com_error:
        # all documents must be closed.
        pass

    # ############################ #
    # Create the top level product #
    # ############################ #

    documents.add("Product")
    doc_root_prod = pia.active_document
    doc_root_prod.save_as(file_name_top)
    product_top = doc_root_prod.product()
    product_top.part_number = "cat_product_1"
    product_top.revision = "A.1"
    product_top.nomenclature = "pycatia product for testing"
    product_top.definition = "pycatia product for testing"

    products = product_top.products

    # ######################## #
    # Create the sub product 1 #
    # ######################## #

    documents.add("Product")
    doc_sub_1 = pia.active_document
    doc_sub_1.save_as(file_name_sub_1)
    product_sub_1 = doc_sub_1.product()
    product_sub_1.part_number = "cat_product_sub_1"
    product_sub_1.revision = "A.1"
    product_sub_1.nomenclature = "pycatia product for testing"

    products_sub_1 = product_sub_1.products
    cat_part_measurable = get_cat_part_measurable()
    documents.open(cat_part_measurable)
    doc_cat_part = pia.active_document
    products_sub_1.add_component(doc_cat_part.product())

    doc_sub_1.save()

    # ######################## #
    # Create the sub product 2 #
    # ######################## #

    documents.add("Product")
    doc_sub_2 = pia.active_document
    doc_sub_2.save_as(file_name_sub_2)
    product_sub_2 = doc_sub_2.product()
    product_sub_2.part_number = "cat_product_sub_2"
    product_sub_2.revision = "A.1"
    product_sub_2.nomenclature = "pycatia product for testing"

    product_sub_2 = product_sub_2.products
    product_sub_2.add_component(doc_cat_part.product())

    doc_sub_2.save()

    # ########################### #
    # Add the sub products to top #
    # ########################### #

    products.add_component(product_sub_1)
    products.add_component(product_sub_1)
    products.add_component(product_sub_2)

    # ################# #
    # Add new component #
    # ################# #

    # yes, the naming seems to arse backwards. ask Dassault why, not me. :-)
    products.add_new_product("ComponentReference")

    # ##################################################################### #
    # Add description instance text to the first product in the top product #
    # ##################################################################### #

    first_product = products.item(1)
    first_product.description_instance = "description instance text"

    doc_root_prod.save()

    # ####################### #
    # close all the documents #
    # ####################### #

    for document in documents.items():
        document.close()


def get_cat_product_top():
    if not source_cat_product.exists():
        pia.logger.info(f"Creating {source_cat_product}")
        create_cat_products(source_cat_product, source_cat_sub_1, source_cat_sub_2)
    return source_cat_product

# ####################### # # close all the documents # # ####################### # # all documents must be closed. # ############################ # # Create the top level product # # ############################ # # ######################## # # Create the sub product 1 # # ######################## # # ######################## # # Create the sub product 2 # # ######################## # # ########################### # # Add the sub products to top # # ########################### # # ################# # # Add new component # # ################# # # yes, the naming seems to arse backwards. ask Dassault why, not me. :-) # ##################################################################### # # Add description instance text to the first product in the top product # # ##################################################################### # # ####################### # # close all the documents # # ####################### #