import flet as ft
import json

class CompanyDetails:
    def __init__(self, page: ft.Page):
        self.page = page
        self.data = self.load_data()
        self.confidentialty_policy = ft.TextField(
            multiline=True, 
            min_lines=5, 
            max_lines=10,
            value=self.data['confidentialty_policy'] if self.data else ''
        )
        self.delivery_policy = ft.TextField(
            multiline=True, 
            min_lines=5, 
            max_lines=10,
            value=self.data['delivery_policy'] if self.data else ''
        )
        self.cancel_policy = ft.TextField(
            multiline=True, 
            min_lines=5, 
            max_lines=10,
            value=self.data['cancel_policy'] if self.data else ''
        )
        self.gdpr = ft.TextField(
            multiline=True, 
            min_lines=5, 
            max_lines=10,
            value=self.data['gdpr'] if self.data else ''
        )
        self.terms_and_conditions = ft.TextField(
            multiline=True, 
            min_lines=5, 
            max_lines=10,
            value=self.data['terms_and_conditions'] if self.data else ''
        )
        self.error_mseeage = ft.Text("")

    def on_save_btn_click(self, e):
        policies = {
            'confidentialty_policy': self.confidentialty_policy.value,
            'delivery_policy': self.delivery_policy.value,
            'cancel_policy': self.cancel_policy.value,
            'gdpr': self.gdpr.value,
            'terms_and_conditions': self.terms_and_conditions.value
        }

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

            self.page.snack_bar = ft.SnackBar(ft.Text('Politicile 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/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 de confidențialitate", size=18, weight=ft.FontWeight.BOLD),
                    self.confidentialty_policy,
                    ft.Divider(height=1),
                    ft.Text("Politica de livrare comandă", size=18, weight=ft.FontWeight.BOLD),
                    self.delivery_policy,
                    ft.Divider(height=1),
                    ft.Text("Politica de anulare comandă", size=18, weight=ft.FontWeight.BOLD),
                    self.cancel_policy,
                    ft.Divider(height=1),
                    ft.Text("Politica GDPR (siguranța datelor cu caracter personal)", size=18, weight=ft.FontWeight.BOLD),
                    self.gdpr,
                    ft.Divider(height=1),
                    ft.Text("Termeni și condiții", size=18, weight=ft.FontWeight.BOLD),
                    self.terms_and_conditions,
                    ft.Text(),
                    self.error_mseeage,
                    ft.Row(
                        [
                            ft.FilledButton("Salveaza", icon = ft.Icons.SAVE, on_click=self.on_save_btn_click)
                        ],
                        alignment=ft.MainAxisAlignment.CENTER
                    )
                    
                ],
                scroll=ft.ScrollMode.ADAPTIVE
            )
        )