from yasi_applets.disks import bp
from flask import Flask, request, session, redirect, \
     url_for, render_template, flash, Blueprint
import dmm.lsblk as lsblk
from flask import current_app

@bp.route('/disks')
def disks_welcome():
    """
    Welcome menu for partitioning
    """
    menu = current_app.config['CONFIG']['settings']['menu_order'].split(" ")
    next_step_url = menu[menu.index("disks")+1]
    if menu[menu.index("disks")] == 1:
        previous_step_url = False
    else:
        previous_step_url = menu[menu.index("disks")-1]

    blockdevs = lsblk.list_block_devices()

    return render_template('disks_welcome.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, blockdevs=blockdevs
                           )


@bp.route('/disks/choose_disk_erase')
def disks_choose_disk_erase():
    """
    Choose a disk to erase
    """
    menu = current_app.config['CONFIG']['settings']['menu_order'].split(" ")
    next_step_url = menu[menu.index("disks")+1]
    if menu[menu.index("disks")] == 1:
        previous_step_url = False
    else:
        previous_step_url = menu[menu.index("disks")-1]

    blockdevs = lsblk.list_block_devices()

    return render_template('disks_erase_menu.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, blockdevs=blockdevs
                           )


@bp.route('/disks/choose_disk_erase_chosen/<disk>')
def disks_choose_disk_erase_confirm(disk):
    """
    Disk chosen, show before and after of disk configuration.
    """

    partitions = current_app.config['CONFIG']['recipe']['create_partitions']

    menu = current_app.config['CONFIG']['settings']['menu_order'].split(" ")
    next_step_url = menu[menu.index("disks")+1]
    if menu[menu.index("disks")] == 1:
        previous_step_url = False
    else:
        previous_step_url = menu[menu.index("disks")-1]
    blockdevs = lsblk.list_block_devices()

    return render_template('disks_erase_confirmed.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, blockdevs=blockdevs,
                           diskname=disk, partitions=partitions
                           )


@bp.route('/disks/advanced')
def disks():
    """
    Disks screen for the webui.
    """
    blockdevs = lsblk.list_block_devices()
    print(lsblk.list_block_devices())
    menu = current_app.config['CONFIG']['settings']['menu_order'].split(" ")
    next_step_url = menu[menu.index("disks")+1]
    if menu[menu.index("disks")] == 1:
        previous_step_url = False
    else:
        previous_step_url = menu[menu.index("disks")-1]

    add_disks_button = '&nbsp; <button class="button iis-info"> <i class="fa fa-hdd-o" aria-hidden="true"></i> &nbsp; Add Disk or Filesystem </button>'
    add_swap_button = '&nbsp; <button class="button iis-info"> <i class="fa fa-object-group" aria-hidden="true"></i> &nbsp; Swap Configuration  </button>'
    bottom_menu = add_disks_button + add_swap_button
    # no bottom menu for this release
    bottom_menu = ""
    build_summary()

    return render_template('disks_advanced.html', blockdevs=blockdevs,
        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,
	partitions=current_app.config['CONFIG']['recipe']['create_partitions']['command_set'],
	format_partitions=current_app.config['CONFIG']['recipe']['format_partitions'],
	bottom_menu=bottom_menu)


@bp.route('/disks/partition/<part>')
def disks_partition(part):
    """
    Partition modal for the webui partition screen.
    """
    print("Partition is: " + part)
    partition = part
    return render_template('disks_partition.html',
        menu=current_app.config['CONFIG']['settings']['menu'],
	partition=partition)


@bp.route('/disks/details/<disk>')
def disks_details(disk):
    """
    Show some details about a disk.
    """
    blockdevs = lsblk.list_block_devices()
    return render_template('disks_details.html', disk=disk, blockdevs=blockdevs)


@bp.route('/disks/wipe_table/<disk>')
def disks_wipe_table(disk):
    """
    Create a new partition table
    """
    return render_template('disks_wipe_partition.html', disk=disk)


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']['disks'] = ("Disks", "/disks", 30)


def build_summary():
    """
    Write up a summary of what this module will do.
    """
    current_app.config['CONFIG']['Summary']['disks'] = {}
    current_app.config['CONFIG']['Summary']['disks']['heading'] = "Disks and Partitions"
    current_app.config['CONFIG']['Summary']['disks']['bleh'] = current_app.config['CONFIG']['recipe']['popcon']['enable_popcon']
    current_app.config['CONFIG']['Summary']['disks']['text'] = "Pre-configured disk layout."
    return("ok?")

build_menu()
