#:import Clock kivy.clock.Clock
#:import Window kivy.core.window.Window
#:import Factory kivy.factory.Factory
#:import _ electrum.gui.kivy.i18n._
#:import KIVY_GUI_PATH electrum.gui.kivy.KIVY_GUI_PATH


###########################
#     Global Defaults
###########################

<Label>
    markup: True
    font_name: 'Roboto'
    font_size: '16sp'
    bound: False
    on_text: if isinstance(self.text, _) and not self.bound: self.bound=True; _.bind(self)

<TextInput>
    on_focus: app._focused_widget = root
    font_size: '18sp'

<Button>
    on_parent: self.MIN_STATE_TIME = 0.1

<ListItemButton>
    font_size: '12sp'

<Carousel>:
    canvas.before:
        Color:
            rgba: 0.1, 0.1, 0.1, 1
        Rectangle:
            size: self.size
            pos: self.pos

<ActionView>:
    canvas.before:
        Color:
            rgba: 0.1, 0.1, 0.1, 1
        Rectangle:
            size: self.size
            pos: self.pos


# Custom Global Widgets

<TopLabel>
    size_hint_y: None
    text_size: self.width, None
    height: self.texture_size[1]

<VGridLayout@GridLayout>:
    rows: 1
    size_hint: 1, None
    height: self.minimum_height



<IconButton@Button>:
    icon: ''
    icon_size: '30dp'
    AnchorLayout:
        pos: self.parent.pos
        size: self.parent.size
        orientation: 'lr-tb'
        Image:
            source: self.parent.parent.icon
            size_hint_x: None
            size: root.icon_size, root.icon_size

            
<BackgroundColor@Widget>
    background_color: 0, 0, 0, 1
    canvas.before:
        Color:
            rgba: root.background_color
        Rectangle:
            size: self.size
            pos: self.pos
<BackgroundTopLabel@TopLabel+BackgroundColor>
    background_color: 0, 0, 0, 1


#########################
#       Dialogs
#########################
<BoxLabel@BoxLayout>
    text: ''
    value: ''
    size_hint_y: None
    height: max(lbl1.height, lbl2.height)
    TopLabel
        id: lbl1
        text: root.text
        pos_hint: {'top':1}
    TopLabel
        id: lbl2
        text: root.value

<BoxButton@BoxLayout>
    text: ''
    value: ''
    size_hint_y: None
    height: max(lbl1.height, lbl2.height)
    TopLabel
        id: lbl1
        text: root.text
        pos_hint: {'top':1}
    Button
        id: lbl2
        text: root.value
        background_color: (0,0,0,0)
        bold: True
        size_hint_y: None
        text_size: self.width, None
        height: self.texture_size[1]
        on_release:
            root.callback()

<OutputItem>
    address: ''
    value: ''
    background_color: 0, 0, 0, 1
    color: 1, 1, 1, 1
    size_hint_y: None
    height: max(lbl1.height, lbl2.height)
    BackgroundTopLabel
        id: lbl1
        text: '[ref=%s]%s[/ref]'%(root.address, root.address)
        color: root.color
        background_color: root.background_color 
        font_size: '6pt'
        shorten: True
        size_hint_x: 0.65
        on_ref_press:
            app._clipboard.copy(root.address)
            app.show_info(_('Address copied to clipboard') + ' ' + root.address)
    TopLabel
        id: lbl2
        text: root.value
        font_size: '6pt'
        size_hint_x: 0.35
        halign: 'right'


<OutputList>
    viewclass: 'OutputItem'
    size_hint: 1, None
    height: min(output_list_layout.minimum_height, dp(144))
    scroll_type: ['bars', 'content']
    bar_width: dp(15)
    RecycleBoxLayout:
        orientation: 'vertical'
        default_size: None, pt(6)
        default_size_hint: 1, None
        size_hint: 1, None
        height: self.minimum_height
        id: output_list_layout
        spacing: '10dp'
        padding: '10dp'
        canvas.before:
            Color:
                rgb: .3, .3, .3
            Rectangle:
                size: self.size
                pos: self.pos

<RefLabel>
    font_size: '6pt'
    name: ''
    data: ''
    visible: True
    opacity: 1 if self.visible else 0
    disabled: not self.visible
    text: self.data if self.data else _('Tap to show')
    touched: False
    padding: '10dp', '10dp'
    background_color: .3, .3, .3, 1
    show_text_with_qr: True
    touch_callback: lambda: app.on_ref_label(self, show_text_with_qr=self.show_text_with_qr)
    on_touch_down:
        touch = args[1]
        touched = bool(self.collide_point(*touch.pos))
        if touched: self.touch_callback()
        if touched: self.touched = True
    canvas.before:
        Color:
            rgba: root.background_color
        Rectangle:
            size: self.size
            pos: self.pos

<TxHashLabel@RefLabel>
    data: ''
    text: ' '.join(map(''.join, zip(*[iter(self.data)]*4))) if self.data else ''

<InfoBubble>
    size_hint: None, None
    width: '270dp' if root.fs else min(self.width, dp(270))
    height: self.width if self.fs else (lbl.texture_size[1] + dp(27))
    BoxLayout:
        padding: '5dp' if root.fs else 0
        Widget:
            size_hint: None, 1
            width: '4dp' if root.fs else '2dp'
        Image:
            id: img
            source: root.icon
            mipmap: True
            size_hint: None, 1
            width: (root.width - dp(20)) if root.fs  else (0 if not root.icon else '32dp')
        Widget:
            size_hint_x: None
            width: '5dp'
        Label:
            id: lbl
            markup: True
            font_size: '12sp'
            text: root.message
            text_size: self.width, None
            valign: 'middle'
            size_hint: 1, 1
            width: 0 if root.fs else (root.width - img.width)


<SendReceiveBlueBottom@GridLayout>
    item_height: dp(42)
    foreground_color: .843, .914, .972, 1
    cols: 1
    padding: '12dp', 0
    canvas.before:
        Color:
            rgba: 0.192, .498, 0.745, 1
        BorderImage:
            source: f'atlas://{KIVY_GUI_PATH}/theming/atlas/light/card_bottom'
            size: self.size
            pos: self.pos


<AddressFilter@GridLayout>
	item_height: dp(42)
	item_width: dp(60)
	foreground_color: .843, .914, .972, 1
	cols: 1
	canvas.before:
		Color:
			rgba: 0.192, .498, 0.745, 1
		BorderImage:
			source: f'atlas://{KIVY_GUI_PATH}/theming/atlas/light/card_bottom'
			size: self.size
			pos: self.pos

<SearchBox@GridLayout>
	item_height: dp(42)
	foreground_color: .843, .914, .972, 1
	cols: 1
	padding: '12dp', 0
	canvas.before:
		Color:
			rgba: 0.192, .498, 0.745, 1
        BorderImage:
            source: f'atlas://{KIVY_GUI_PATH}/theming/atlas/light/card_bottom'
            size: self.size
            pos: self.pos

<CardSeparator@Widget>
    size_hint: 1, None
    height: dp(1)
    color: .909, .909, .909, 1
    canvas:
        Color:
            rgba: root.color if root.color else (0, 0, 0, 0)
        Rectangle:
            size: self.size
            pos: self.pos

<CardItem@ButtonBehavior+BoxLayout>
    size_hint: 1, None
    height: '65dp'
    group: 'requests'
    padding: dp(12)
    spacing: dp(5)
    screen: None
    on_release: self.screen.show_item(args[0])
    canvas.before:
        Color:
            rgba: (0.192, .498, 0.745, 1) if self.state == 'down' else (0.15, 0.15, 0.17, 1)
        Rectangle:
            size: self.size
            pos: self.pos

<BlueButton@Button>:
    background_color: 1, .585, .878, 0
    halign: 'left'
    text_size: (self.width-10, None)
    size_hint: 0.5, None
    default_text: ''
    text: self.default_text
    padding: '5dp', '5dp'
    height: '40dp'
    text_color: self.foreground_color
    disabled_color: 1, 1, 1, 1
    foreground_color: 1, 1, 1, 1
    canvas.before:
        Color:
            rgba: (0.9, .498, 0.745, 1) if self.state == 'down' else self.background_color
        Rectangle:
            size: self.size
            pos: self.pos


<KButton@Button>:
    size_hint: 1, None
    height: '60dp'
    font_size: '30dp'
    on_release:
        self.parent.update_amount(self.text)


<StripLayout>
    padding: 0, 0, 0, 0

<TabbedPanelStrip>:
    on_parent:
        if self.parent: self.parent.bar_width = 0
        if self.parent: self.parent.scroll_x = 0.5


<TabbedCarousel>
    carousel: carousel
    do_default_tab: False
    Carousel:
        anim_type: 'out_quart'
        min_move: .05
        anim_move_duration: .1
        anim_cancel_duration: .54
        on_index: root.on_index(*args)
        id: carousel



<CleanHeader@TabbedPanelHeader>
    border: 16, 0, 16, 0
    markup: False
    text_size: self.size
    halign: 'center'
    valign: 'middle'
    bold: True
    font_size: '12.5sp'
    background_normal: f'atlas://{KIVY_GUI_PATH}/theming/atlas/light/tab_btn'
    background_down: f'atlas://{KIVY_GUI_PATH}/theming/atlas/light/tab_btn_pressed'


<ColoredLabel@Label>:
    font_size: '48sp'
    color: (.6, .6, .6, 1)
    canvas.before:
        Color:
            rgb: (.9, .9, .9)
        Rectangle:
            pos: self.x + sp(2), self.y + sp(2)
            size: self.width - sp(4), self.height - sp(4)


<SettingsItem@ButtonBehavior+BoxLayout>
    orientation: 'vertical'
    title: ''
    description: ''
    size_hint: 1, None
    height: '60dp'
    action: lambda x: None
    canvas.before:
        Color:
            rgba: (0.192, .498, 0.745, 1) if self.state == 'down' else (0.3, 0.3, 0.3, 0)
        Rectangle:
            size: self.size
            pos: self.pos
    on_release:
        Clock.schedule_once(self.action)
    Widget
    TopLabel:
        id: title
        text: self.parent.title
        bold: True
        halign: 'left'
    TopLabel:
        text: self.parent.description
        color: 0.8, 0.8, 0.8, 1
        halign: 'left'
    Widget




<ScreenTabs@Screen>
    TabbedCarousel:
        id: panel
        tab_height: '48dp'
        tab_width: panel.width/3
        strip_border: 0, 0, 0, 0
        SendScreen:
            id: send_screen
            tab: send_tab
        HistoryScreen:
            id: history_screen
            tab: history_tab
        ReceiveScreen:
            id: receive_screen
            tab: receive_tab
        CleanHeader:
            id: send_tab
            text: _('Send')
            slide: 0
        CleanHeader:
            id: history_tab
            text: _('History')
            slide: 1
        CleanHeader:
            id: receive_tab
            text: _('Receive')
            slide: 2


<ActionOvrButton@ActionButton>
    #on_release:
        # fixme: the following line was commented out because it does not seem to do what it is intended
        # Clock.schedule_once(lambda dt: self.parent.parent.dismiss() if self.parent else None, 0.05)
    on_press:
        Clock.schedule_once(lambda dt: app.popup_dialog(self.name), 0.05)
        self.state = 'normal'

<NetworkDialog@BoxLayout>
    orientation: 'vertical'
    ScrollView:
        GridLayout:
            id: scrollviewlayout
            cols:1
            size_hint: 1, None
            height: self.minimum_height
            padding: '10dp'
            SettingsItem:
                value: _("{} connections.").format(app.num_nodes) if app.num_nodes else _("Not connected")
                title: _("Status") + ': ' + self.value
                description: _("Connections with Electrum servers")

            CardSeparator
            SettingsItem:
                title: _("Server") + ': ' + app.server_host
                description: _("Server used to query your history.")
                action: lambda x: app.popup_dialog('server')

            CardSeparator
            SettingsItem:
                title: _("Proxy") + ': ' + app.proxy_str
                description: _('Proxy configuration')
                action: lambda x: app.popup_dialog('proxy')

            CardSeparator
            SettingsItem:
                title: _("Auto-connect") + ': ' + ('ON' if app.auto_connect else 'OFF')
                description: _("Select your server automatically")
                action: app.toggle_auto_connect

            CardSeparator
            SettingsItem:
                title: _("One-server mode") + ': ' + ('ON' if app.oneserver else 'OFF')
                description: _("Only connect to a single server")
                action: app.toggle_oneserver
                disabled: app.auto_connect and not app.oneserver

            CardSeparator
            SettingsItem:
                value: "%d blocks" % app.num_blocks
                title: _("Blockchain") + ': ' + self.value
                description: _('Verified block headers')

            CardSeparator
            SettingsItem:
                title: _('Fork detected at block {}').format(app.blockchain_forkpoint) if app.num_chains>1 else _('No fork detected')
                fork_description: (_('You are following branch') if app.auto_connect else _("Your server is on branch")) + ' ' + app.blockchain_name
                description: self.fork_description if app.num_chains>1 else _('Connected nodes are on the same chain')
                action: app.choose_blockchain_dialog
                disabled: app.num_chains == 1


BoxLayout:
    orientation: 'vertical'

    canvas.before:
        Color:
            rgb: .6, .6, .6
        Rectangle:
            size: self.size
            source: f'{KIVY_GUI_PATH}/data/background.png'

    ActionBar:

        ActionView:
            id: av
            ActionPrevious:
                with_previous: False
                size_hint: None, None
                size: 0, 0

            ActionButton:
                size_hint_x: None
                text: app.wallet_name
                bold: True
                color: 0.7, 0.7, 0.7, 1
                font_size: '22dp'
                on_release:
                    Clock.schedule_once(lambda dt: app.popup_dialog('status'), 0.05)
                    self.state = 'normal'

            ActionButton:
                size_hint_x: 0.8
                text: ''
                opacity:0

            ActionOverflow:
                id: ao
                size_hint_x: 0.2
                ActionOvrButton:
                    name: 'about'
                    text: _('About')
                ActionOvrButton:
                    name: 'wallets'
                    text: _('Wallets')
                ActionOvrButton:
                    name: 'network'
                    text: _('Network')
                    disabled: app.network is None
                ActionOvrButton:
                    name: 'addresses_dialog'
                    text: _('Addresses')
                ActionOvrButton:
                    name: 'lightning_channels_dialog'
                    text: _('Channels')
                ActionOvrButton:
                    name: 'settings'
                    text: _('Settings')
                    on_parent:
                        # when widget overflow drop down is shown, adjust the width
                        parent = args[1]
                        if parent: ao._dropdown.width = sp(200)

    ScreenManager:
        id: manager
        ScreenTabs:
            id: tabs
