import flet as ft
import requests


from navigation.ba import NavigationBA
from navigation.user import NavigationUser

class Home:
    def __init__(self, page: ft.Page):
        self.page = page
        self.user = self.page.session.store.get('user')
        self.navigation_ba = NavigationBA(self.page, self)
        self.navigation_user = NavigationUser(self.page, self)
        
        self.placeholder = ft.Container(
            content=self.build_documente_juridice(),
            padding=10,
            expand=True,
        )

    def build_destinations(self):
        if self.user['role'] == 'user':
            return self.navigation_user.build()
        if self.user['role'] == 'ba':
            return self.navigation_ba.build()

    def build_documente_juridice(self):
        if self.user['role'] == 'user':
            return self.navigation_user.build_documente_juridice()
        if self.user['role'] == 'ba':
            return self.navigation_ba.build_documente_juridice()

    async def on_nav_changed(self, e):
        if self.user['role'] == 'user':
            return await self.navigation_user.on_nav_change(e)
        if self.user['role'] == 'ba':
            return await self.navigation_ba.on_nav_change(e)
        

    async def build(self):
        return ft.Container(
            content=ft.Row(
                [
                    ft.NavigationRail(
                        selected_index=0,
                        label_type=ft.NavigationRailLabelType.ALL,
                        min_width=100,
                        min_extended_width=400,
                        leading=ft.Image(src='images/logo_juridic_block.png', width=100),
                        group_alignment=-0.9,
                        #extended=True,
                        destinations=self.build_destinations(),
                        on_change = self.on_nav_changed,
                    ),
                    ft.VerticalDivider(width=1),
                    self.placeholder,
                ],
                expand=True
            ),
            expand=True
        )