import sqlite3

import click
from pick import pick

from ..util import pick_with_cancel

def _get_reward_types(connection):
    command = 'SELECT id, description FROM reward_types ORDER BY description'
    return connection.execute(command).fetchall()

def select_reward_type_id(connection):
    reward_types = _get_reward_types(connection)
    selection = pick([reward_type['description']
                      for reward_type in reward_types],
                     'Select a reward type:')
    return reward_types[selection[1]]['id']

def list_reward_types(connection):
    click.echo_via_pager('\n'.join(
        [reward_type['description']
         for reward_type in _get_reward_types(connection)]
    ))

def add_reward_type(connection):
    new_reward_type = click.prompt('Please enter a new reward type')
    command = 'INSERT INTO reward_types (description) VALUES (?)'
    with connection:
        try:
            connection.execute(command, (new_reward_type,))
        except sqlite3.IntegrityError:
            click.secho(
                'Failed to add duplicate reward type: ' + new_reward_type,
                fg='red')
            exit(1)

def remove_reward_type(connection):
    title = 'Please select a reward type.'
    reward_types = [reward_type['description']
                    for reward_type in _get_reward_types(connection)]
    if not reward_types:
        click.secho('There is no reward type to remove.', fg='red')
        return
    selection = pick_with_cancel(title, reward_types)
    if selection:
        command = 'DELETE FROM reward_types WHERE description = ?'
        with connection:
            connection.execute(command, (selection[0],))
        click.echo('Removed the reward type: ' + selection[0])

def update_reward_type(connection):
    title = 'Please select a reward type.'
    reward_types = [reward_type['description']
                    for reward_type in _get_reward_types(connection)]
    if not reward_types:
        click.secho('There is no reward type to update.', fg='red')
        return
    selection = pick_with_cancel(title, reward_types)
    if selection:
        new_reward_type = click.prompt('Please enter a new reward type')
        command = ('UPDATE reward_types SET description = ? '
                   'WHERE description = ?')
        with connection:
            connection.execute(command, (new_reward_type, selection[0]))

@@ -43,16 +43,46 @@
 ort pick
+%0Afrom tabulate import tabulate
 %0A%0Afrom .
@@ -110,16 +110,43 @@
 h_cancel
+, successful_update_message
 %0A%0Adef _g
@@ -630,49 +630,36 @@
     
-click.echo_via_pager('%5Cn'.join(%0A        %5B
+reward_types = %5B%5Bindex + 1, 
 rewa
@@ -672,33 +672,45 @@
 e%5B'description'%5D
-%0A
+%5D%0A           
          for rew
@@ -697,32 +697,39 @@
              for
+ index,
  reward_type in 
@@ -716,36 +716,66 @@
 dex, reward_type
- in 
+%0A                    in enumerate(
 _get_reward_type
@@ -787,22 +787,88 @@
 nection)
+)
 %5D%0A    
+click.echo_via_pager(tabulate(reward_types, tablefmt='fancy_grid'
 ))%0A%0Adef 
@@ -1679,32 +1679,173 @@
   if selection:%0A
+        if not click.confirm('Remove ' + selection%5B0%5D +%0A                             ' and all of its related rewards?'):%0A            return%0A
         command 
@@ -1987,24 +1987,25 @@
       click.
+s
 echo('Remove
@@ -2039,16 +2039,28 @@
 ction%5B0%5D
+, fg='green'
 )%0A%0Adef u
@@ -2485,35 +2485,35 @@
 enter a new 
-reward type
+description
 ')%0A        c
@@ -2707,8 +2707,44 @@
 on%5B0%5D))%0A
+        successful_update_message()%0A

Clean up reward-types implementation