from yasi_applets.software import bp
from flask import Flask, request, session, redirect, \
     url_for, render_template, flash, Blueprint
from flask import current_app

@bp.route('/software')
def webui_software():
    """
    Software screen for the webui.
    """
    menu = current_app.config['CONFIG']['settings']['menu_order'].split(" ")
    next_step_url = menu[menu.index("software")+1]
    if menu[menu.index("software")] == 1:
        previous_step_url = False
    else:
        previous_step_url = menu[menu.index("software")-1]

    sources_button = '&nbsp; <button class="button iis-info"> <i class="fa fa-cloud-download" aria-hidden="true"></i> &nbsp; Edit Sources </button>'
    blends_button = '&nbsp; <button class="button iis-info"> <i class="fa fa-download" aria-hidden="true"></i> &nbsp; Install a Blend  </button>'
    bottom_menu = sources_button + blends_button
    # Leave out useless buttons until they do more
    bottom_menu = ''
    build_summary()

    return render_template('software.html',
        menu=current_app.config['CONFIG']['settings']['menu'],
	menu_order=current_app.config['CONFIG']['settings']['menu_order'].split(),
	previous_step = previous_step_url,
	next_step = next_step_url,
	bottom_menu = bottom_menu,
	popcon=current_app.config['CONFIG']['recipe']['popcon']['enable_popcon'],
	desktop=current_app.config['CONFIG']['recipe']['install_desktop_environment']['packages'])


@bp.route('/software/settings', methods=['GET', 'POST'])
def software_settings():
    """
    Receive settings for the software applet.
    """
    if request.method == 'POST':
        popcon = "popcon" in request.form
        current_app.config['CONFIG']['recipe']['popcon']['enable_popcon'] = popcon
        print(current_app.config['CONFIG']['recipe']['popcon']['enable_popcon'])
        build_summary()
        return ('', 204)


@bp.route('/software/select-desktop', methods=['GET', 'POST'])
def software_select_desktop():
    """
    Select a desktop environment for installation
    """
    if request.method == 'POST':
        desktop = request.form['software-desktop']
        print(desktop)
        current_app.config['CONFIG']['recipe']['install_desktop_environment']['packages'] = desktop
        build_summary()
        return ('', 204)


def build_menu():
    """
    Define menu items and paths.
    """
    # proper one once translations are done:
    #current_app.config['CONFIG']['settings']['menu']['welcome'] = (build_stringlist()['menu_item'], "/welcome", 10)
    current_app.config['CONFIG']['settings']['menu']['software'] = ("Software", "/software", 40)


def build_summary():
    """
    Write up a summary of what this module will do.
    """
    current_app.config['CONFIG']['Summary']['software'] = {}
    current_app.config['CONFIG']['Summary']['software']['heading'] = "Software"
    current_app.config['CONFIG']['Summary']['software']['bleh'] = current_app.config['CONFIG']['recipe']['popcon']['enable_popcon']
    current_app.config['CONFIG']['Summary']['software']['text'] = "Participate in Popularity Contest: " + str(current_app.config['CONFIG']['recipe']['popcon']['enable_popcon']) + "<br/>Desktop environment selected: " + str(current_app.config['CONFIG']['recipe']['install_desktop_environment']['packages'])
    return("ok?")


build_menu()
