import os
import argparse
from pathlib import Path

from pygarment.meshgen.boxmeshgen import BoxMesh
from pygarment.meshgen.simulation import run_sim
import pygarment.data_config as data_config
from pygarment.meshgen.sim_config import PathCofig


def get_command_args():
    """command line arguments to control the run"""
    # https://stackoverflow.com/questions/40001892/reading-named-command-arguments
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--pattern_spec', '-p', 
        help='pattern specification JSON file. File name should end with "_specification.json"', 
        type=str, 
        default='./assets/Patterns/shirt_mean_specification.json')
    parser.add_argument(
        "--body", "-b", type=str, 
        choices=['mean_all', 'mean_female', 'mean_male', 'f_smpl_average_A40', 'm_smpl_average_A40', 'nana'], 
        default='mean_all', help="Body to use.")
    parser.add_argument(
        '--sim_config', '-s', 
        help='Path to simulation config', 
        type=str, 
        default='./assets/Sim_props/default_sim_props.yaml')

    args = parser.parse_args()
    print('Commandline arguments: ', args)

    return args


if __name__ == "__main__":

    args = get_command_args()

    props = data_config.Properties(args.sim_config) 
    props.set_section_stats('sim', fails={}, sim_time={}, spf={}, fin_frame={}, body_collisions={}, self_collisions={})
    props.set_section_stats('render', render_time={})

    spec_path = Path(args.pattern_spec)
    garment_name, _, _ = spec_path.stem.rpartition('_')  # assuming ending in '_specification'

    sys_props = data_config.Properties('./system.json')
    paths = PathCofig(
        in_element_path=spec_path.parent,  
        out_path=sys_props['output'], 
        in_name=garment_name,
        body_name=args.body,    # 'f_smpl_average_A40'
        smpl_body=False,   # NOTE: depends on chosen body model
        add_timestamp=True
    )

    # Generate and save garment box mesh (if not existent)
    print(f"Generate box mesh of {garment_name} with resolution {props['sim']['config']['resolution_scale']}...")
    print('\nGarment load: ', paths.in_g_spec)

    garment_box_mesh = BoxMesh(paths.in_g_spec, props['sim']['config']['resolution_scale'])
    garment_box_mesh.load()
    garment_box_mesh.serialize(
        paths, store_panels=False, uv_config=props['render']['config']['uv_texture'])

    props.serialize(paths.element_sim_props)

    run_sim(
        garment_box_mesh.name, 
        props, 
        paths,
        save_v_norms=False,
        store_usd=False,  # NOTE: False for fast simulation!
        optimize_storage=False,   # props['sim']['config']['optimize_storage'],
        verbose=False
    )
    
    props.serialize(paths.element_sim_props)
