import flet as ft
from dbActions.fidelity_cards import FidelityCards as FC

class FidelityCards:
    def __init__(self, page: ft.Page):
        self.page = page
        self.fidelity_cards_manager = FC()

        self.all_cards = self.fidelity_cards_manager.get_all_fidelity_cards()
        self.list_of_cars = ft.ListView(
            controls=self.create_list(self.all_cards, self.on_delete_btn_click, self.on_edit_btn_click),
            expand=True,
            spacing=10
        )

        self.search_bar = ft.TextField(label = "Cauta dupa numar de telefon", on_submit=self.on_search_btn_click, expand=True)
        self.search_btn = ft.IconButton(icon=ft.Icons.SEARCH, on_click=self.on_search_btn_click)

        self.card_id = ft.TextField(label="Id card")
        self.card_type = ft.Dropdown(
            label='Reducere (%)',
            options=[
                ft.dropdown.Option(key='10', text="10%"),
                ft.dropdown.Option(key='20', text="20%"),
                ft.dropdown.Option(key='30', text="30%"),
            ],
            expand=True
        )
        self.client_name = ft.TextField(label="Nume client")
        self.phone_number = ft.TextField(label="Numar telefon")

        self.item_to_be_edit = None
        self.add_dialog = ft.AlertDialog(
            title=ft.Text(value = "Adauga card de fidelitate" if self.item_to_be_edit == None else "Actualizeaza card de fidelitate"),
            content=ft.Column(
                [
                    self.card_id,
                    self.card_type,
                    self.client_name,
                    self.phone_number
                ],
                height=250,
                width=400
            ),
            actions = [
                ft.Button("Salveaza", on_click=self.on_save_btn_click, icon=ft.Icons.SAVE),
                ft.TextButton("Anuleaza", on_click=self.on_cancel_btn_click, icon=ft.Icons.CANCEL)
            ]
        )

        self.delete_dialog = ft.AlertDialog(
            title=ft.Text("Setergeti cardul?"),
            actions = [
                ft.Button("Da", on_click=self.on_condifm_delete_btn_click, icon=ft.Icons.DELETE),
                ft.TextButton("Nu", on_click=self.on_cancel_delete_btn_click, icon=ft.Icons.CANCEL)
            ]
        )

        self.item_to_be_delete = None


    def on_condifm_delete_btn_click(self, e):
        self.fidelity_cards_manager.remove_fidelity_card(self.item_to_be_delete)
        self.page.close(self.delete_dialog)
        self.update_list()
        self.item_to_be_delete = None

    def on_cancel_delete_btn_click(self, e):
        self.page.close(self.delete_dialog)
        self.item_to_be_delete = None

    def on_save_btn_click(self, e):
        card_id = self.card_id.value
        card_type = self.card_type.value
        client_name = self.client_name.value
        phone_number = self.phone_number.value

        if self.item_to_be_edit == None:
            self.fidelity_cards_manager.add_fidelity_card(
                card_id,
                card_type,
                client_name,
                phone_number
            )
        else:
            self.fidelity_cards_manager.update_card(
                self.item_to_be_edit,
                card_id,
                card_type,
                client_name,
                phone_number
            )
            self.item_to_be_edit = None

        #set to defualt
        self.card_id.value = ''
        self.card_id.update()
        self.card_type.value = ''
        self.card_type.update()
        self.client_name.value = ''
        self.client_name.update()
        self.phone_number.value = ''
        self.phone_number.update()

        #close popup
        self.page.close(self.add_dialog)

        #update_list
        self.update_list()

    def update_list(self):
        self.all_cards = self.fidelity_cards_manager.get_all_fidelity_cards()
        self.list_of_cars.controls = self.create_list(
            self.all_cards, 
            self.on_delete_btn_click, 
            self.on_edit_btn_click
        )
        self.list_of_cars.update()

    def on_cancel_btn_click(self, e):
        self.page.close(self.add_dialog)
        self.item_to_be_edit = None

    def on_add_btn_click(self, e):
        self.page.open(self.add_dialog)

    def create_list(self, items, on_click_handler2, on_click_handler3):
        return [
            ft.Container(
                content=ft.Row(
                    [

                        ft.Column(
                            [
                                ft.Text(value=f"Nume: {item['client_name']}", weight=ft.FontWeight.BOLD),
                                ft.Row(
                                    [
                                        ft.Text(value=f"Id card: {item['card_id']};", size=12),
                                        ft.Text(value=f"Reducere: {item['card_type']}%;", size=12),
                                        ft.Text(value=f"Telefon: {item['phone_number']}", size=12)
                                    ],
                                    spacing=10
                                )
                            ]
                        ),
             
                        ft.Row(
                            [
                                ft.IconButton(
                                    icon=ft.Icons.EDIT,
                                    on_click=lambda e, id=item: on_click_handler3(id),
                                ),
                                ft.IconButton(
                                    icon=ft.Icons.DELETE,
                                    on_click=lambda e, id=item['id']: on_click_handler2(id), 
                                    icon_color=ft.Colors.RED,
                                ),
                            ]
                        )
                        
                    ],
                    alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
                ),
                width=300,
                bgcolor=ft.Colors.BROWN_50,
                border = ft.border.all(1, ft.Colors.GREY),
                padding=10,
                border_radius=8,
            )
            for item in items
        ]

    def on_search_btn_click(self, e):
        print(self.search_bar.value)
        
        for card in self.all_cards:
            buffer = []
            if self.search_bar.value in card['phone_number']:
                buffer.append(card)
        self.list_of_cars.controls = self.create_list(
            buffer, 
            self.on_delete_btn_click, 
            self.on_edit_btn_click
        )
        self.list_of_cars.update()

        #set to default
        self.search_bar.value = ''
        self.search_bar.update()

    def on_delete_btn_click(self, id):
        self.item_to_be_delete = id
        self.page.open(self.delete_dialog) 

    def on_edit_btn_click(self, item):
        self.page.open(self.add_dialog)
        self.item_to_be_edit = item['id']
        self.card_id.value = item['card_id']
        self.card_id.update()
        self.card_type.value = item['card_type']
        self.card_type.update()
        self.client_name.value = item['client_name']
        self.client_name.update()
        self.phone_number.value = item['phone_number']
        self.phone_number.update()

    def build(self):
        return ft.Container(
            content=ft.Column(
                [
                    ft.Row(
                        [
                            ft.Text("Card de fidelitate", size=18, weight=ft.FontWeight.BOLD),
                            ft.Button("Adauga", icon=ft.Icons.ADD, on_click=self.on_add_btn_click),
                        ],
                        alignment=ft.MainAxisAlignment.SPACE_BETWEEN
                    ),
                    ft.Row(
                        [
                            self.search_bar,
                            self.search_btn
                        ]
                    ),
                    self.list_of_cars
                ],
                alignment=ft.MainAxisAlignment.START,
                expand=True
            ),
            expand=True
        )