""" this is generic report format for DFSS Test
 run - python3 html_template.py"""
from matplotlib import pyplot as plt
from datetime import datetime
import numpy as np
import os.path
from os import path
import pdfkit
# dev complete
def report_banner(date):
    banner_data = """
                   
                    
                    
                    
                    
                    DFS Testing Report                        
                    
                    
                    
                    
                 """
    return str(banner_data)
# dev complete
def test_objective(
        objective="The DFS Test is designed to test the Performance of the Netgear Access Point.Dynamic frequency selection is a technology that is designed to ensure that wireless devices operating in the unlicensed WLAN 5 GHz bands are able to detect when they may be interfering with military and weather radar systems and automatically switch over to another frequency where they will not cause any disturbance. "):
    test_objective = """
                    
                    Objective
 
                    """ + str(objective) + """
                    
                    """
    return str(test_objective)
# dev complete
def test_setup_information(ap_name="", ssid="", num_client=1):
    setup_information = """
                        
                        
                            
                              | Test Setup Information | 
                            
                            
                              | Device Under Test | 
                              
                                
                                  
                                    | AP Name | 
                                    """ + ap_name + """ | 
                                   
                                  
                                    | SSID | 
                                    """ + ssid + """ | 
                                   
                                  
                                    | Number of Clients | 
                                    """ + num_client + """ | 
                                   
                                 
                               | 
                            
                        
                        
                        """
    return str(setup_information)
# yet to test on dev level
def graph_html(graph_path=""):
    graph_html_obj = """
    
    Detection Time Graph
 
      
    
    """
    return str(graph_html_obj)
def bar_plot(ax, data, colors=None, total_width=0.8, single_width=1, legend=True):
    # Check if colors where provided, otherwhise use the default color cycle
    if colors is None:
        colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
    # Number of bars per group
    n_bars = len(data)
    # The width of a single bar
    bar_width = total_width / n_bars
    # List containing handles for the drawn bars, used for the legend
    bars = []
    # Iterate over all data
    for i, (name, values) in enumerate(data.items()):
        # The offset in x direction of that bar
        x_offset = (i - n_bars / 2) * bar_width + bar_width / 2
        # print(values)
        # Draw a bar for every value of that type
        for x, y in enumerate(values):
            bar = ax.bar(x + x_offset, y, width=bar_width * single_width, color=colors[i % len(colors)])
        # Add a handle to the last drawn bar, which we'll need for the legend
        bars.append(bar[0])
    # Draw legend if we need
    if legend:
        ax.legend(bars, data.keys(), bbox_to_anchor=(1.1, 1.05))
    ax.set_ylabel('Time in seconds')
    ax.set_xlabel('Channels')
    # ax.set_xticks(1)
    channels = [52, 56, 60, 64, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140]
    idx = np.asarray([i for i in range(len(channels))])
    ax.set_xticks(idx)
    ax.set_xticklabels(
        ('52', '56', '60', '64', '100', '104', '108', '112', '116', '120', '124', '128', '132', '136', '140'))
def generate_graph(result_data, graph_path):
    detection_data = {
        "FCC0": result_data['FCC0']['detection_time_lst'],
        "FCC1": result_data['FCC1']['detection_time_lst'],
        "FCC2": result_data['FCC2']['detection_time_lst'],
        "FCC3": result_data['FCC3']['detection_time_lst'],
        "FCC4": result_data['FCC4']['detection_time_lst'],
        "FCC5": result_data['FCC5']['detection_time_lst'],
        "ETSI1": result_data['ETSI1']['detection_time_lst'],
        "ETSI2": result_data['ETSI2']['detection_time_lst'],
        "ETSI3": result_data['ETSI3']['detection_time_lst'],
        "ETSI4": result_data['ETSI4']['detection_time_lst'],
        "ETSI5": result_data['ETSI5']['detection_time_lst'],
        "ETSI6": result_data['ETSI6']['detection_time_lst']
    }
    fig, ax = plt.subplots()
    bar_plot(ax, detection_data, total_width=.8, single_width=1.2)
    my_dpi = 96
    figure = plt.gcf()  # get current figure
    figure.set_size_inches(18, 6)
    # when saving, specify the DPI
    str(datetime.now()).split(",")[0].replace(" ", "-").split(".")[0]
    plt.savefig(graph_path + "/image.png", dpi=my_dpi)
    return str(graph_html(graph_path + "/image.png"))
# yet to test on dev level
def add_radar_table(result_data):
    radar_html_struct = dict.fromkeys(list(result_data.keys()))
    for fcc in list(result_data.keys()):
        fcc_type = result_data[fcc]["radar_lst"]
        final_data = ""
        for i in fcc_type:
            if i == "YES":
                final_data = final_data + "YES | "
            else:
                final_data = final_data + "NO | "
        radar_html_struct[fcc] = final_data
    radar_html = """
                
                
                  
                    | Radar Detected  | 
                  
                  
                    
                      | 
                       | 
                      52 | 
                      56 | 
                      60 | 
                      64 | 
                      100 | 
                      104 | 
                      108 | 
                      112 | 
                      116 | 
                      120 | 
                      124 | 
                      128 | 
                      132 | 
                      136 | 
                      140 | 
                    
                    
                      | FCC0 | 
                      
                      """ + radar_html_struct["FCC0"] + """
                    
                      | FCC1 | 
                      """ + radar_html_struct["FCC1"] + """
                    
                      FCC2 | 
                      """ + radar_html_struct["FCC2"] + """
                    
                    
                      | FCC3 | 
                      """ + radar_html_struct["FCC3"] + """
                    
                    
                      | FCC4 | 
                      """ + radar_html_struct["FCC4"] + """
                    
                    
                      | FCC5 | 
                      """ + radar_html_struct["FCC5"] + """
                    
                    
                      | ETSI1 | 
                      """ + radar_html_struct["ETSI1"] + """
                    
                    
                      | ETSI2 | 
                      """ + radar_html_struct["ETSI2"] + """
                    
                    
                      | ETSI3 | 
                      """ + radar_html_struct["ETSI3"] + """
                    
                    
                      | ETSI4 | 
                      """ + radar_html_struct["ETSI4"] + """
                    
                    
                      | ETSI5 | 
                      """ + radar_html_struct["ETSI5"] + """
                    
                    
                      | ETSI6 | 
                      """ + radar_html_struct["ETSI6"] + """
                    
                  
                
                
                """
    return str(radar_html)
# yet to test on dev level
def add_client_cx_table(result_data):
    client_html_struct = dict.fromkeys(list(result_data.keys()))
    for fcc in list(result_data.keys()):
        fcc_type = result_data[fcc]["connection_time_lst"]
        final_data = ""
        for i in fcc_type:
            final_data = final_data + "" + str(i) + " | "
        client_html_struct[fcc] = final_data
    client_cx_html = """
                     
                    
                      
                        | Client Connection Time (sec) | 
                      
                      
                        
                        | 
                       | 
                      52 | 
                      56 | 
                      60 | 
                      64 | 
                      100 | 
                      104 | 
                      108 | 
                      112 | 
                      116 | 
                      120 | 
                      124 | 
                      128 | 
                      132 | 
                      136 | 
                      140 | 
                    
                    
                      | FCC0 | 
                      
                      """ + client_html_struct["FCC0"] + """
                    
                      | FCC1 | 
                      """ + client_html_struct["FCC1"] + """
                    
                      FCC2 | 
                      """ + client_html_struct["FCC2"] + """
                    
                    
                      | FCC3 | 
                      """ + client_html_struct["FCC3"] + """
                    
                    
                      | FCC4 | 
                      """ + client_html_struct["FCC4"] + """
                    
                    
                      | FCC5 | 
                      """ + client_html_struct["FCC5"] + """
                    
                    
                      | ETSI1 | 
                      """ + client_html_struct["ETSI1"] + """
                    
                    
                      | ETSI2 | 
                      """ + client_html_struct["ETSI2"] + """
                    
                    
                      | ETSI3 | 
                      """ + client_html_struct["ETSI3"] + """
                    
                    
                      | ETSI4 | 
                      """ + client_html_struct["ETSI4"] + """
                    
                    
                      | ETSI5 | 
                      """ + client_html_struct["ETSI5"] + """
                    
                    
                      | ETSI6 | 
                      """ + client_html_struct["ETSI6"] + """
                    
                  
                
                
                     """
    return str(client_cx_html)
# yet to test on dev level
def add_detection_table(result_data):
    detection_html_struct = dict.fromkeys(list(result_data.keys()))
    for fcc in list(result_data.keys()):
        fcc_type = result_data[fcc]["detection_time_lst"]
        final_data = ""
        for i in fcc_type:
            final_data = final_data + "" + str(i) + "  | "
        detection_html_struct[fcc] = final_data
    detection_html = """
                    
                    
                      
                        | Detection Time (sec) | 
                      
                      
                        
                        | 
                       | 
                      52 | 
                      56 | 
                      60 | 
                      64 | 
                      100 | 
                      104 | 
                      108 | 
                      112 | 
                      116 | 
                      120 | 
                      124 | 
                      128 | 
                      132 | 
                      136 | 
                      140 | 
                    
                    
                      | FCC0 | 
                      
                      """ + detection_html_struct["FCC0"] + """
                    
                      | FCC1 | 
                      """ + detection_html_struct["FCC1"] + """
                    
                      FCC2 | 
                      """ + detection_html_struct["FCC2"] + """
                    
                    
                      | FCC3 | 
                      """ + detection_html_struct["FCC3"] + """
                    
                    
                      | FCC4 | 
                      """ + detection_html_struct["FCC4"] + """
                    
                    
                      | FCC5 | 
                      """ + detection_html_struct["FCC5"] + """
                    
                    
                      | ETSI1 | 
                      """ + detection_html_struct["ETSI1"] + """
                    
                    
                      | ETSI2 | 
                      """ + detection_html_struct["ETSI2"] + """
                    
                    
                      | ETSI3 | 
                      """ + detection_html_struct["ETSI3"] + """
                    
                    
                      | ETSI4 | 
                      """ + detection_html_struct["ETSI4"] + """
                    
                    
                      | ETSI5 | 
                      """ + detection_html_struct["ETSI5"] + """
                    
                    
                      | ETSI6 | 
                      """ + detection_html_struct["ETSI6"] + """
                    
                  
                
                
                    """
    return detection_html
# yet to test on dev level
def add_switched_channel_table(result_data):
    switched_html_struct = dict.fromkeys(list(result_data.keys()))
    for fcc in list(result_data.keys()):
        fcc_type = result_data[fcc]["switched_ch_lst"]
        final_data = ""
        for i in fcc_type:
            final_data = final_data + "" + i + " | "
        switched_html_struct[fcc] = final_data
    switched_data = """
                    
                    
                      
                        | Switched Channel | 
                      
                      
                        
                        | 
                       | 
                      52 | 
                      56 | 
                      60 | 
                      64 | 
                      100 | 
                      104 | 
                      108 | 
                      112 | 
                      116 | 
                      120 | 
                      124 | 
                      128 | 
                      132 | 
                      136 | 
                      140 | 
                    
                    
                      | FCC0 | 
                      
                      """ + switched_html_struct["FCC0"] + """
                    
                      | FCC1 | 
                      """ + switched_html_struct["FCC1"] + """
                    
                      FCC2 | 
                      """ + switched_html_struct["FCC2"] + """
                    
                    
                      | FCC3 | 
                      """ + switched_html_struct["FCC3"] + """
                    
                    
                      | FCC4 | 
                      """ + switched_html_struct["FCC4"] + """
                    
                    
                      | FCC5 | 
                      """ + switched_html_struct["FCC5"] + """
                    
                    
                      | ETSI1 | 
                      """ + switched_html_struct["ETSI1"] + """
                    
                    
                      | ETSI2 | 
                      """ + switched_html_struct["ETSI2"] + """
                    
                    
                      | ETSI3 | 
                      """ + switched_html_struct["ETSI3"] + """
                    
                    
                      | ETSI4 | 
                      """ + switched_html_struct["ETSI4"] + """
                    
                    
                      | ETSI5 | 
                      """ + switched_html_struct["ETSI5"] + """
                    
                    
                      | ETSI6 | 
                      """ + switched_html_struct["ETSI6"] + """
                    
                  
                
                
                    """
    return switched_data
def generate_report(result_data=None,
                    date=None,
                    ap_name="testap",
                    ssid="lexusdut",
                    num_client="1",
                    graph_path="/home/lanforge/html-reports/dfs"):
    date = str(datetime.now()).split(",")[0].replace(" ", "-").split(".")[0]
    """result_data = {'FCC0': {'switched_ch_lst': ['64', '140', '124', '108', '108', '108', '112', '40', '40', '120', '124', '128', '140', '136', '140'], 'detection_time_lst': [4, 5, 4, 5, 0, 4, 4, 4, 4, 0, 0, 0, 5, 0, 0], 'radar_lst': ['YES', 'YES', 'YES', 'YES', 'NO', 'YES', 'YES', 'YES', 'YES', 'NO', 'NO', 'NO', 'YES', 'NO', 'NO'],
                            'connection_time_lst': [73, 75, 614, 66, 0, 65, 74, 0, 0, 0, 0, 0, 66, 0, 0]},
                   'FCC1': {'switched_ch_lst': ['108', '132', '116', '120', '120', '100', '136', '124', '60', '120', '124', '128', '136', '136', '140'], 'detection_time_lst': [4, 4, 4, 4, 0, 5, 4, 4, 4, 0, 0, 0, 4, 0, 0], 'radar_lst': ['YES', 'YES', 'YES', 'YES', 'NO', 'YES', 'YES', 'YES', 'YES', 'NO', 'NO', 'NO', 'YES', 'NO', 'NO'],
                            'connection_time_lst': [67, 65, 64, 613, 0, 73, 67, 623, 74, 0, 0, 0, 74, 0, 0]},
                   'FCC2': {'switched_ch_lst': ['52', '56', '60', '64', '64', '104', '108', '112', '116', '120', '124', '128', '132', '136', '140'], 'detection_time_lst': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'radar_lst': ['NO', 'NO', 'NO', 'NO', 'NO', 'NO', 'NO', 'NO', 'NO', 'NO', 'NO', 'NO', 'NO', 'NO', 'NO'],
                            'connection_time_lst': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]},
                   'FCC3': {'switched_ch_lst': ['140', '128', '140', '40', '40', '100', '136', '112', '36', '120', '124', '128', '124', '136', '140'], 'detection_time_lst': [5, 5, 4, 4, 0, 4, 4, 0, 4, 0, 0, 0, 5, 0, 0], 'radar_lst': ['YES', 'YES', 'YES', 'YES', 'NO', 'YES', 'YES', 'NO', 'YES', 'NO', 'NO', 'NO', 'YES', 'NO', 'NO'],
                            'connection_time_lst': [67, 615, 75, 0, 0, 67, 74, 0, 0, 0, 0, 0, 618, 0, 0]},
                   'FCC4': {'switched_ch_lst': [], 'detection_time_lst': [], 'radar_lst': [],
                            'connection_time_lst': []},
                   'FCC5': {'switched_ch_lst': [], 'detection_time_lst': [], 'radar_lst': [],
                            'connection_time_lst': []},
                   'ETSI1': {'switched_ch_lst': [], 'detection_time_lst': [], 'radar_lst': [],
                             'connection_time_lst': []},
                   'ETSI2': {'switched_ch_lst': [], 'detection_time_lst': [],
                             'radar_lst': [],
                             'connection_time_lst': []},
                   'ETSI3': {'switched_ch_lst': [], 'detection_time_lst': [], 'radar_lst': [],
                             'connection_time_lst': []},
                   'ETSI4': {'switched_ch_lst': [], 'detection_time_lst': [],
                             'radar_lst': [],
                             'connection_time_lst': []},
                   'ETSI5': {'switched_ch_lst': [], 'detection_time_lst': [],
                             'radar_lst': [],
                             'connection_time_lst': []},
                   'ETSI6': {'switched_ch_lst': [], 'detection_time_lst': [], 'radar_lst': [],
                             'connection_time_lst': []}}"""
    for i in result_data:
        print(i)
    reports_root = graph_path + "/" + date
    if path.exists(graph_path):
        os.mkdir(reports_root)
        print("Reports Root is Created")
    else:
        os.mkdir(graph_path)
        os.mkdir(reports_root)
        print("Reports Root is created")
    print("Generating Reports in : ", reports_root)
    html_report = \
        report_banner(date) + \
        test_objective() + \
        test_setup_information(ap_name, ssid, num_client) + \
        generate_graph(result_data, reports_root) + \
        add_radar_table(result_data) + \
        add_client_cx_table(result_data) + \
        add_detection_table(result_data) + \
        add_switched_channel_table(result_data)
    # print(html_report)
    # write the html_report into a file in /home/lanforge/html_reports in a directory named DFS_TEST and html_report name should be having a timesnap with it
    f = open(reports_root + "/report.html", "a")
    f.write(html_report)
    f.close()
    # write logic to generate pdf here
    pdfkit.from_file(reports_root + "/report.html", reports_root + "/report.pdf")
# test blocks from here
if __name__ == '__main__':
    generate_report()
    # generate_graph()