import flet as ft
import json

class AboutUS:
    def __init__(self, page:ft.Page):
        self.page = page
        self.data = self.load_data()
        self.company_name = ft.Text(value=self.data['company_name'])
        self.vat = ft.Text(value=self.data['vat'])
        self.register_number = ft.Text(value=self.data['register_number'])
        self.address = ft.Text(value=self.data['address'])

    def load_data(self):
        try:
            with open('instance/company_data.json', 'r', encoding='utf-8') as f:
                data = json.load(f)
            return data
        except FileNotFoundError:
            return {
                'company_name': '',
                'vat': '',
                'register_number': '',
                'address': ''
            }
        except json.JSONDecodeError:
            self.page.snack_bar = ft.SnackBar(ft.Text('Eroare: fișierul company_data.json este corupt.'))
            self.page.snack_bar.open = True
            self.page.update()
            return None
        except Exception as ex:
            self.page.snack_bar = ft.SnackBar(ft.Text(f'Eroare la citirea datelor companiei: {ex}'))
            self.page.snack_bar.open = True
            self.page.update()
            return None
        
    def build(self):
        return ft.Container(
            content=ft.Row(
                [
                    ft.Image(src="images/tainagustului.png", width=100),
                    ft.Column(
                        [
                            ft.Text("Despre noi", size=18, weight=ft.FontWeight.BOLD),
                            self.company_name,
                            self.vat,
                            self.register_number,
                            self.address
                        ]
                    )
                ],
                alignment=ft.MainAxisAlignment.CENTER,
                vertical_alignment=ft.CrossAxisAlignment.CENTER,
                scroll=ft.ScrollMode.ADAPTIVE
            )
        )