import flet as ft
import json

class CompanyData:
    def __init__(self, page: ft.Page):
        self.page = page
        self.data =self.load_data()
        self.comapny_name = ft.TextField(label = "Denumire firma:", value=self.data['company_name'] if self.data else '')
        self.vat = ft.TextField(label="CUI", value=self.data['vat'] if self.data else '')
        self.register_number = ft.TextField(label="Numar registru comert", value=self.data['register_number'] if self.data else '')
        self.company_address = ft.TextField(label="Sediu", value=self.data['address'] if self.data else '')
        self.error_mseeage = ft.Text("")

    def on_save_btn_click(self, e):
        
        data = {
            'company_name': self.comapny_name.value,
            'vat': self.vat.value,
            'register_number': self.register_number.value,
            'address': self.company_address.value
        }

        try:
            with open('instance/company_data.json', 'w', encoding='utf-8') as f:
                json.dump(data, f, ensure_ascii=False, indent=4)

            self.page.snack_bar = ft.SnackBar(ft.Text('Datele companiei au fost salvate cu succes!'))
            self.page.snack_bar.open = True
            self.page.update()
            self.error_mseeage.value = "Datele au fost salvate cu succes!"
            self.error_mseeage.update()
        except Exception as ex:
            self.page.snack_bar = ft.SnackBar(ft.Text(f'Eroare la salvare: {ex}'))
            self.page.snack_bar.open = True
            self.page.update()

    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.Column(
                [
                    ft.Text("Despre noi:", size=18, weight=ft.FontWeight.BOLD),
                    self.comapny_name,
                    self.vat,
                    self.register_number,
                    self.company_address,
                    ft.Text(),
                    ft.Row(
                        [
                            ft.FilledButton("Salveaza", icon=ft.Icons.SAVE, on_click=self.on_save_btn_click)
                        ],
                        alignment=ft.MainAxisAlignment.CENTER
                    )
                    
                ]
            )
        )