# -*- coding: utf8 -*-

# Copyright (C) 2015 - Philipp Temminghoff <phil65@kodi.tv>
# This program is Free Software see LICENSE file for details

import xbmcgui
import xbmc

from kodi65 import addon

C_LIST_SIMPLE = 3
C_LIST_DETAIL = 6
C_BUTTON_GET_MORE = 5
C_LABEL_HEADER = 1


class SelectDialog(xbmcgui.WindowXMLDialog):

    def __init__(self, *args, **kwargs):
        xbmcgui.WindowXMLDialog.__init__(self)
        self.items = kwargs.get('listing')
        self.header = kwargs.get('header')
        self.detailed = kwargs.get('detailed')
        self.extrabutton = kwargs.get('extrabutton')
        self.listitems = [i.get_listitem() for i in self.items] if self.items else []
        self.index = -1

    def onInit(self):
        self.list = self.getControl(C_LIST_DETAIL)
        self.getControl(C_LIST_DETAIL).setVisible(self.detailed)
        self.getControl(C_LIST_SIMPLE).setVisible(not self.detailed)
        self.getControl(C_BUTTON_GET_MORE).setVisible(bool(self.extrabutton))
        if self.extrabutton:
            self.getControl(C_BUTTON_GET_MORE).setLabel(self.extrabutton)
        self.getControl(C_LABEL_HEADER).setLabel(self.header)
        self.list.addItems(self.listitems)
        self.setFocus(self.list)

    def onClick(self, control_id):
        if control_id in [C_LIST_SIMPLE, C_LIST_DETAIL]:
            self.index = int(self.list.getSelectedPosition())
        elif control_id == C_BUTTON_GET_MORE:
            self.index = -2
        self.close()

    def onFocus(self, control_id):
        pass


def open(listitems, header, detailed=True, extrabutton=False):
    """
    open selectdialog, return index (-1 for closing, -2 for extra button)
    """
    xbmc.executebuiltin("Dialog.Close(busydialog)")
    w = SelectDialog('DialogSelect.xml', addon.PATH,
                     listing=listitems,
                     header=header,
                     detailed=detailed,
                     extrabutton=extrabutton)
    w.doModal()
    return w.index

# -*- coding: utf8 -*-

# Copyright (C) 2015 - Philipp Temminghoff <phil65@kodi.tv>
# This program is Free Software see LICENSE file for details

import xbmcgui
import xbmc

from kodi65 import addon

C_LIST_SIMPLE = 3
C_LIST_DETAIL = 6
C_BUTTON_GET_MORE = 5
C_LABEL_HEADER = 1


class SelectDialog(xbmcgui.WindowXMLDialog):

    def __init__(self, *args, **kwargs):
        xbmcgui.WindowXMLDialog.__init__(self)
        self.items = kwargs.get('listing')
        self.header = kwargs.get('header')
        self.detailed = kwargs.get('detailed')
        self.extrabutton = kwargs.get('extrabutton')
        self.listitems = [i.get_listitem() for i in self.items] if self.items else []
        self.index = -1

    def onInit(self):
        if not self.listitems:
            self.index == -1
            self.close()
        elif len(self.listitems) == 1:
            self.index == 0
            self.close()
        self.list = self.getControl(C_LIST_DETAIL)
        self.getControl(C_LIST_DETAIL).setVisible(self.detailed)
        self.getControl(C_LIST_SIMPLE).setVisible(not self.detailed)
        self.getControl(C_BUTTON_GET_MORE).setVisible(bool(self.extrabutton))
        if self.extrabutton:
            self.getControl(C_BUTTON_GET_MORE).setLabel(self.extrabutton)
        self.getControl(C_LABEL_HEADER).setLabel(self.header)
        self.list.addItems(self.listitems)
        self.setFocus(self.list)

    def onClick(self, control_id):
        if control_id in [C_LIST_SIMPLE, C_LIST_DETAIL]:
            self.index = int(self.list.getSelectedPosition())
        elif control_id == C_BUTTON_GET_MORE:
            self.index = -2
        self.close()

    def onFocus(self, control_id):
        pass


def open(listitems, header, detailed=True, extrabutton=False):
    """
    open selectdialog, return index (-1 for closing, -2 for extra button)
    """
    xbmc.executebuiltin("Dialog.Close(busydialog)")
    w = SelectDialog('DialogSelect.xml', addon.PATH,
                     listing=listitems,
                     header=header,
                     detailed=detailed,
                     extrabutton=extrabutton)
    w.doModal()
    return w.index

exit early in case selectdialog itemcount <= 1