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 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_config_cpe_lyon_batA():
    ESW1_CPE_LYON_BAT_A_data= load_json_data_from_file(file_path='data/ESW1_CPE_LYON_BAT_A.json')
    ESW1_CPE_LYON_BAT_A_config = render_network_config(template_name='vlan_switch.j2', data=ESW1_CPE_LYON_BAT_A_data)

    R2_LYON_BAT_A_data = load_json_data_from_file(file_path='data/R2_CPE_LYON_BAT_A.json')
    R2_LYON_BAT_A_config = render_network_config(template_name='vlan_router.j2', data=R2_LYON_BAT_A_data)

    R1_LYON_BAT_A_data = load_json_data_from_file(file_path='data/R1_CPE_LYON_BAT_A.json')
    R1_LYON_BAT_A_config = render_network_config(template_name='vlan_router.j2', data=R1_LYON_BAT_A_data)

    R2_LYON_BAT_A_config_vrrp = render_network_config(template_name='vrrp_router.j2', data=R2_LYON_BAT_A_data)
    R1_LYON_BAT_A_config_vrrp = render_network_config(template_name='vrrp_router.j2', data=R1_LYON_BAT_A_data)

    return {
        'esw1': ESW1_CPE_LYON_BAT_A_config,
        'r1': R1_LYON_BAT_A_config,
        'r2': R2_LYON_BAT_A_config,
        'r1_vrrp' : R1_LYON_BAT_A_config_vrrp,
        'r2_vrrp' : R2_LYON_BAT_A_config_vrrp
    }

def create_config_cpe_lyon_batB():
    ESW1_CPE_LYON_BAT_B_data= load_json_data_from_file(file_path='data/ESW1_CPE_LYON_BAT_B.json')
    ESW1_CPE_LYON_BAT_B_config = render_network_config(template_name='vlan_switch.j2', data=ESW1_CPE_LYON_BAT_B_data)

    R2_LYON_BAT_B_data = load_json_data_from_file(file_path='data/R2_CPE_LYON_BAT_B.json')
    R2_LYON_BAT_B_config = render_network_config(template_name='vlan_router.j2', data=R2_LYON_BAT_B_data)

    R1_LYON_BAT_B_data = load_json_data_from_file(file_path='data/R1_CPE_LYON_BAT_B.json')
    R1_LYON_BAT_B_config = render_network_config(template_name='vlan_router.j2', data=R1_LYON_BAT_B_data)

    R2_LYON_BAT_B_config_vrrp = render_network_config(template_name='vrrp_router.j2', data=R2_LYON_BAT_B_data)
    R1_LYON_BAT_B_config_vrrp = render_network_config(template_name='vrrp_router.j2', data=R1_LYON_BAT_B_data)
    return {
        'esw1': ESW1_CPE_LYON_BAT_B_config,
        'r1': R1_LYON_BAT_B_config,
        'r2': R2_LYON_BAT_B_config,
        'r1_vrrp' : R1_LYON_BAT_B_config_vrrp,
        'r2_vrrp' : R2_LYON_BAT_B_config_vrrp   
    }
    
if __name__ == "__main__":
    """
        process question 3 to 5:
    """
    #question 3:
    config = create_config_cpe_lyon_batA()

    #question 4:
    save_built_config('config/R1_CPE_LYON_BAT_A.conf', config.get('r1'))
    save_built_config('config/R2_CPE_LYON_BAT_A.conf', config.get('r2'))
    save_built_config('config/ESW1_CPE_LYON_BAT_A.conf', config.get('esw1'))
    save_built_config('config/R1_CPE_LYON_BAT_A_VRRP.conf', config.get('r1_vrrp'))
    save_built_config('config/R2_CPE_LYON_BAT_A_VRRP.conf', config.get('r2_vrrp'))

    #question 5:
    config = create_config_cpe_lyon_batB()
    save_built_config('config/R1_CPE_LYON_BAT_B.conf', config.get('r1'))
    save_built_config('config/R2_CPE_LYON_BAT_B.conf', config.get('r2'))
    save_built_config('config/ESW1_CPE_LYON_BAT_B.conf', config.get('esw1'))
    save_built_config('config/R1_CPE_LYON_BAT_B_VRRP.conf', config.get('r1_vrrp'))
    save_built_config('config/R2_CPE_LYON_BAT_B_VRRP.conf', config.get('r2_vrrp'))
    