import flet as ft
import json

class GDPR:
    def __init__(self, page: ft.Page):
        self.page = page

    def load_data(self):
        try:
            with open('instance/policies.json', 'r', encoding='utf-8') as f:
                data = json.load(f)
            return data
        except FileNotFoundError:
            return {
                'confidentialty_policy': '',
                'delivery_policy': '',
                'cancel_policy': '',
                'gdpr': '',
                'terms_and_conditions': ''
            }
        except json.JSONDecodeError:
            self.page.snack_bar = ft.SnackBar(ft.Text('Eroare: fișierul policies.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 politicilor: {ex}'))
            self.page.snack_bar.open = True
            self.page.update()
            return None

    def build(self):
        return ft.Container(
            content=ft.Column(
                [
                    ft.Text("Politica GDPR (siguranța datelor cu caracter personal)",size = 18, weight=ft.FontWeight.BOLD, text_align=ft.TextAlign.CENTER),
                    ft.Text(value=self.load_data()['gdpr'])
                ],
                alignment=ft.MainAxisAlignment.START,
                scroll=ft.ScrollMode.ADAPTIVE
            ),
            padding=10,
            expand=True
        )