#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Contact :   liuyuqi.gov@msn.cn
@Time    :   2023/12/29 19:12:38
@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
@Desc    :   
'''

import flet as ft
from flet import (
    Checkbox,
    Column,
    FloatingActionButton,
    IconButton,
    OutlinedButton,
    Page,
    Row,
    Tab,
    Tabs,
    Text,
    TextField,
    UserControl,
    colors,
    icons,
)
from flet.plotly_chart import PlotlyChart
import logging

from .pages.home import HomePage
from .pages.plotly_chart import ChartPage
from .pages.todo import TodoPage

class App(UserControl):
    ''' build a flet app '''

    def __init__(self):
        ''' init '''
        super().__init__()
        log_format = "%(asctime)s - %(levelname)s - %(message)s"
        logging.basicConfig(filename='app.log', level=logging.DEBUG, format=log_format)
        logging.info('程序启动')

    @staticmethod
    def main(page: ft.Page):
        ''' main page '''
        # page.title = "ToDo App"
        # page.horizontal_alignment = "center"
        # page.scroll = "adaptive"
        # page.update()
        # page.add(app)

        content = HomePage()
        def menu_changed(e):
            ''' menu change evnet '''
            content.controls.clear()
            if e.control.selected_index == 0:
                content.controls.append(HomePage())
            elif e.control.selected_index == 1:
                content.controls.append(TodoPage())
            elif e.control.selected_index == 2:
                content.controls.append(ft.Text("商品库!"))
            elif e.control.selected_index == 3:
                fig = ChartPage().DrawChart()
                # fig.show()
                content.controls.append(PlotlyChart(fig, expand=True))
            elif e.control.selected_index == 4:
                content.controls.append(ft.Text("Setting!"))
            page.update()

        rail = ft.NavigationRail(
            selected_index=0,
            label_type=ft.NavigationRailLabelType.ALL,
            # extended=True,
            min_width=100,
            min_extended_width=400,
            # leading=ft.FloatingActionButton(icon=ft.icons.CREATE, text="Add"),
            group_alignment=-0.9,
            destinations=[
                ft.NavigationRailDestination(
                    icon=ft.icons.HOME, selected_icon=ft.icons.HOME, label="首页"
                ),
                ft.NavigationRailDestination(
                    icon_content=ft.Icon(ft.icons.FAVORITE),
                    selected_icon_content=ft.Icon(name=ft.icons.FAVORITE, color=ft.colors.PINK),
                    label="关键词",
                ),
                ft.NavigationRailDestination(
                    icon_content=ft.Icon(ft.icons.SHOPIFY),
                    selected_icon_content=ft.Icon(name=ft.icons.SHOPIFY, color=ft.colors.PINK),
                    label="商品库",
                ),
                ft.NavigationRailDestination(
                    icon_content=ft.Icon(ft.icons.BAR_CHART),
                    selected_icon_content=ft.Icon(name=ft.icons.BAR_CHART, color=ft.colors.PINK),
                    label="数据分析",
                ),
                ft.NavigationRailDestination(
                    icon=ft.icons.SETTINGS_OUTLINED,
                    selected_icon_content=ft.Icon(ft.icons.SETTINGS),
                    label_content=ft.Text("Settings"),
                ),
            ],
            on_change=menu_changed,
        )
        page.title = 'First App'
        page.add(
            ft.Row(
                [
                    rail,
                    ft.VerticalDivider(width=1),
                    content,
                ],
                expand=True,
            )
        )

    def run(self):
        ''''''
        ft.app(target=self.main, view=ft.WEB_BROWSER, host="0.0.0.0", port=8080)