#!/usr/bin/env python3

import easymacro as app


ID_EXTENSION = ''
_ = None

PREFIX = 'fav'


class Controllers(object):
    TITLE = ''

    def __init__(self, dlg):
        self.d = dlg
        self.paths = []

    def button_add_action(self, event):
        path = app.paths.get_file()
        if not path:
            return
        if path in self.paths:
            msg = _('Path previously added')
            app.msgbox(msg, self.TITLE)
            return

        self.paths.append(path)
        p = app.paths(path)
        path_img = app.paths.join(self.d.path, 'images/delete.svg')
        image = app.paths.image(path_img)
        self.d.grid.add_row((p.file_name, image, path))
        self.d.grid.set_cell_tooltip(0, self.d.grid.row_count-1, p.path)
        self.d.grid.sort(0)
        return

    def button_save_action(self, event):
        msg = _('Want you save your favorites files?')
        if not app.question(msg, self.TITLE):
            return

        base = f'service:{ID_EXTENSION}?{{}}'
        submenus = []
        for i in range(self.d.grid.row_count):
            name = self.d.grid[0, i]
            path = self.d.grid[2, i]
            label = f'{i+1}. {name}'
            url = app.paths.to_url(path)
            sm = {'Label': label, 'CommandURL': base.format(url)}
            submenus.append(sm)
        sm = {'Label': '-'}
        submenus.append(sm)
        sm = {'Label': _('Favorites...'), 'CommandURL': base.format('config')}
        submenus.append(sm)

        command = 'menu.zaz.favorites'
        data = {
            'Label': 'Favorites',
            'CommandURL': command,
            'After': '.uno:RecentFileList',
            'Submenu': submenus,
        }
        for application in ('main', 'calc', 'writer'):
            app.menus[application].insert('file', data)

        app.set_config('paths', self.paths, PREFIX)

        self.d.close(1)
        msg = _('Favorites saved correctly')
        app.msgbox(msg, self.TITLE)
        return

    def grid_click(self, event):
        if not self.d.grid.is_valid:
            return

        col = self.d.grid.column
        row = self.d.grid.row
        if col != 1:
            return

        file_name = self.d.grid[0, row]
        path = self.d.grid[2, row]
        msg = _('Want you delete this file?\n\n')
        if app.question(msg + file_name, self.TITLE):
            self.paths.remove(path)
            self.d.grid.remove_row(row)
        return

    def grid_double_click(self, event):
        if not self.d.grid.is_valid:
            return

        col = self.d.grid.column
        row = self.d.grid.row
        if col != 0:
            return

        app.msgbox(self.d.grid.get_cell_tooltip(col, row))
        return


def _config():
    dlg = _create_dialog()
    path_img = app.paths.join(dlg.path, 'images/delete.svg')
    image = app.paths.image(path_img)

    paths = app.get_config('paths', prefix=PREFIX)
    for i, path in enumerate(paths):
        p = app.paths(path)
        dlg.grid.add_row((p.file_name, image, path))
        dlg.grid.set_cell_tooltip(0, i, p.path)

    if paths:
        dlg.grid.sort(0)
        dlg.events.paths = paths
    dlg.open()
    return


def _create_dialog():
    TITLE = _('ZAZ Favorites')

    args = {
        'Name': 'dialog',
        'Title': TITLE,
        'Width': 160,
        'Height': 170,
    }
    dlg = app.create_dialog(args)
    dlg.id = ID_EXTENSION
    dlg.events = Controllers
    dlg.events.TITLE = TITLE

    args = {
        'Type': 'Button',
        'Name': 'button_add',
        'Label': _('~Add'),
        'ImageURL': 'add.svg',
        'ImagePosition': 1,
        'Width': 60,
        'Height': 20,
        'Y': 5,
    }
    dlg.add_control(args)

    columns = (
        {'Title': _('File Name'), 'MaxWidth': 120},
        {'Title': '-', 'HorizontalAlign': 1, 'Resizeable': False, 'ColumnWidth': 10},
        {'Title': '', 'Resizeable': False, 'ColumnWidth': 0},
    )
    args = {
        'Type': 'Grid',
        'Name': 'grid',
        'Width': 140,
        'Height': 110,
        'ShowRowHeader': True,
    }
    grid = dlg.add_control(args)
    grid.columns = columns

    args = {
        'Type': 'Button',
        'Name': 'button_save',
        'Label': _('~Save'),
        'ImageURL': 'save.svg',
        'ImagePosition': 1,
        'Width': 60,
        'Height': 20,
    }
    dlg.add_control(args)

    dlg.button_add.center()
    dlg.grid.move(dlg.button_add)
    dlg.button_save.move(dlg.grid)
    dlg.grid.center()
    dlg.button_save.center()

    return dlg


def run(args, path_locales):
    global _

    _ = app.install_locales(path_locales)

    if args == 'config':
        _config()
        return

    app.docs.open(args)
    return
