import flet as ft
from dataForm import FormData

class Form(ft.Container):
    def __init__(self):
        super().__init__(
            expand=True,
            border=ft.border.all(width=1, color="white10"),
            padding=20,
            border_radius=6,
            bgcolor=ft.colors.with_opacity(opacity=0.61, color="grey100")
        )
          
        self.selected_tags=[]
        self.tags = ft.Row(controls=self.render_tags(), wrap=True )
        
        self.content = ft.ListView(
            auto_scroll=True,
            spacing=20,
            expand=True,
            controls=[
                ft.Text(value="pilih jenis pelaporan :", size=17, weight="w800"),
                self.tags,
                ft.Divider(height=10, color="transparent"),
            ]
        )
    
    def reset_tags(self):
        for tag_container in self.tags.controls:
            tag_container.bgcolor = "grey100"
            tag_container.update()
        self.selected_tags = []
    
      # create methode render tags
    def render_tags(self):
        return [
            ft.Container(
                content=ft.Text(value=name, weight="w500"),
                bgcolor=ft.colors.with_opacity(opacity=0.61, color="grey"),
                padding=6,
                border_radius=4,
                animate= ft.Animation(duration=600, curve="decelerate"),
                #on click
                on_click=lambda _, name=name, bgcolor=bgcolor: self.update_selected_tags(name, bgcolor),
            )
            for name, bgcolor in FormData.tags
        ]
    
    def update_selected_tags(self, name, bgcolor):
        if name in self.selected_tags:
            self.selected_tags.remove(name)
            self.update_tag_color(name, ft.colors.with_opacity(opacity=0.61, color="grey"))
        else:
            self.selected_tags.append(name)
            self.update_tag_color(name, ft.colors.with_opacity(opacity=0.61, color="green"))
        print("ini:", self.selected_tags)
        
    def update_tag_color(self, name, bgcolor):
    # Temukan kontainer tag yang sesuai dengan nama
        for tag_container in self.tags.controls:
            if tag_container.content.value == name:
                tag_container.bgcolor = bgcolor
                tag_container.update()
                break

    def get_selected_tags(self):
        return self.selected_tags