import json
from jinja2 import Template, Environment,  FileSystemLoader

env = Environment(loader=FileSystemLoader("templates"))

def load_json_data_from_file(file_path):
    with open(file_path) as json_file:
        data = json.load(json_file)
    return data


def load_yaml_data_from_file(file_path):
    with open(file_path) as yaml_file:
        data = yaml.safe_load(yaml_file)
    return data


def render_network_config(template_name, data):
    template = env.get_template(template_name)
    return template.render(data)


def save_built_config(file_name, data):
    with open(file_name, "w") as f:
        f.write(data)
    return file_name

def create_vlan_config_cpe_marseille():
    """
        Must return two values : router config and the switch config
    """
    esw2_data = load_json_data_from_file(file_path='data/vlan_ESW2.json')
    esw2_config = render_network_config(template_name='vlan_switch.j2', data=esw2_data)

    R2_data = load_json_data_from_file(file_path='data/vlan_R02.json')
    R2_config = render_network_config(template_name='vlan_router.j2', data=R2_data)
    return R2_config,esw2_config


def create_vlan_config_cpe_paris():
    """
        Must return two values : router config and the switch config
    """
    esw3_data = load_json_data_from_file(file_path='data/vlan_ESW3.json')
    esw3_config = render_network_config(template_name='vlan_switch.j2', data=esw3_data)

    R3_data = load_json_data_from_file(file_path='data/vlan_R03.json')
    R3_config = render_network_config(template_name='vlan_router.j2', data=R3_data)
    return R3_config,esw3_config

def create_ospf_config_cpe_marseille():
    R1_data = load_json_data_from_file(file_path='data/ospf_R01.json')
    R1_config = render_network_config(template_name='config_ospf.j2', data=R1_data)

    return R1_config

def create_ospf_config_cpe_paris():
    R2_data = load_json_data_from_file(file_path='data/ospf_R02.json')
    R2_config = render_network_config(template_name='config_ospf.j2', data=R2_data)

    return R2_config

def create_ospf_config_cpe_lyon():
    R3_data = load_json_data_from_file(file_path='data/ospf_R03.json')
    R3_config = render_network_config(template_name='config_ospf.j2', data=R3_data)

    return R3_config

if __name__ == "__main__":
    """
        process question 1 to 5:
    """
    r02_config, esw2_config = create_vlan_config_cpe_marseille()
    save_built_config('config/vlan_R02.conf', r02_config)
    save_built_config('config/vlan_ESW2.conf', esw2_config)
    
    r03_config, esw3_config = create_vlan_config_cpe_paris()
    save_built_config('config/vlan_R03.conf', r03_config)
    save_built_config('config/vlan_ESW3.conf', esw3_config)

    R1_ospf_config = create_ospf_config_cpe_marseille()
    save_built_config('config/ospf_R1.conf', R1_ospf_config)
    R2_ospf_config = create_ospf_config_cpe_paris()
    save_built_config('config/ospf_R2.conf', R2_ospf_config)
    R3_ospf_config = create_ospf_config_cpe_lyon()
    save_built_config('config/ospf_R3.conf', R3_ospf_config)


