from yasi_applets.disks import bp
from flask import Flask, request, session, redirect, \
     url_for, render_template, flash, Blueprint
import dmm.lsblk as lsblk
import gettext
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()
    global string_dict
    string_dict = build_stringlist()
    next_step_url = False

    select_button = '<button class="button is-dark"> <span style="color: #ffa45c;"> </span> Select an option above</button>'
    bottom_menu_right = select_button


    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,
                           string_dict = string_dict, 
                           bottom_menu_right = bottom_menu_right
                           )


@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()

    next_step_url = False
    select_button = '<button class="button is-dark"> <span style="color: #ffa45c;"> </span> Select a disk</button>'
    bottom_menu_right = select_button


    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,
                           bottom_menu_right = bottom_menu_right,
                           string_dict = build_stringlist()
                           )


@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(" ")
    for index, disk_part in enumerate(current_app.config['CONFIG']['recipe']['create_partitions']['command_set']):
        if "nvme" in disk or "mmc" in disk or "loop" in disk:
            partspec = "p"
        else:
            partspec = ""
        current_app.config['CONFIG']['recipe']['create_partitions']['command_set'][index]["device"] = disk + partspec + str(index)
    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,
                           string_dict = build_stringlist()
                           )


@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()
    global string_dict
    string_dict = build_stringlist()
    print("SD: ", string_dict)

    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, string_dict=string_dict)


@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,  string_dict = build_stringlist())


@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,
                           string_dict = build_stringlist())


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


@bp.route('/disks/choose_partition_erase/')
def disk_wipe_partition():
    """
    Select a partition to erase
    """
    blockdevs = lsblk.list_block_devices()
    return render_template('disks_wipe_part_select.html', disk='',
                           blockdevs = blockdevs,
                           string_dict = build_stringlist())


def build_stringlist():
    """
    Return all the strings that is used in this applet.
    """
    string_dict = {}
    string_dict['menu_item'] = _("Disks")
    string_dict['welcome_text'] =  ("How would you like to install Debian?")
    string_dict['install_next_text'] = _("Install next to existing partition")
    string_dict['replace_part_text'] = _("Replace a partition")
    string_dict['erase_disk_text'] = _("Erase entire disk")
    string_dict['advanced_part_text'] = _("Advanced partitioning")
    string_dict['preconfigured_part_text'] = _("Preconfigured Partitioning")
    string_dict['detected_disks_text'] = _("The Following devices have been detected:")
    string_dict['confirm_disk_erase'] = _("Please confirm the disk to erase:")
    string_dict['disk_text'] = _("disk")
    string_dict['serial_text'] = _("serial")
    string_dict['select_disk_text'] = _("Select Disk")
    string_dict['partition_text'] = _("Partition")
    string_dict['type_text'] = _("Type")
    string_dict['mount_point_text'] = _("Mount Point")
    string_dict['label_text'] = _("Label")
    string_dict['size_text'] = _("Size")
    string_dict['used_text'] = _("Used")
    string_dict['disk_details_text'] = _("Disk Details")
    string_dict['wipe_disk_text'] = _("Wipe Disk")
    string_dict['new_partition_text'] = _("New Partition")
    string_dict['replicate_layout_text'] = _("Replicate Layout")
    string_dict['diagnostics_text'] = _("Diagnostics")
    string_dict['proposed_part_text'] = _("Proposed Partitioning")
    string_dict['confirm_text'] = _("Confirm")
    return string_dict


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()
