"""
Copyright 2023-2026 Jobin Rezai

This file is part of LVNAuth.

LVNAuth is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

LVNAuth is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with LVNAuth.  If not, see <https://www.gnu.org/licenses/>.
"""

import tkinter as tk
import shared_components
from tkinter import ttk, IntVar


class LoadingWindow(tk.Toplevel):
    
    def __init__(self,
                 master=None,
                 loading_text="Please wait...",
                 mode="indeterminate",
                 **kw):
        
        super().__init__(master, **kw)

        self.loading_text = loading_text
        
        self.geometry("320x100")

        # self["background"] = config.default_bg # "lightgrey"
        self.title("")
        
        # 'indeterminate' or 'determinate'
        self.mode = mode
        
        self.style_loading = ttk.Style(self)
        self.style_loading.configure("TFrame", background=self["background"])
        self.style_loading.configure("TLabel", background=self["background"])
        self.style_loading.configure("TProgressbar", background="blue")
        
        self.create_frame_main().pack(fill="x", expand=True, padx=15, pady=5)
        self.create_frame_loading_text().grid(row=0, column=0)
        self.create_frame_progress().grid(row=1, column=0)
        
        # Hide maximum button and make the window non-resizable.
        self.resizable(0, 0)
        
        # Prevents a 'taskbar' icon from showing up just for this window 
        # and also makes it on-top of the master.
        self.transient(master)
        

        # Center the window to in the monitor that it's on
        shared_components.Passer.center_window_active_monitor(self)        

    
    def create_frame_main(self):
        
        self.frame_main = ttk.Frame(self)
        self.frame_main.grid_rowconfigure(0, weight=1)
        self.frame_main.grid_columnconfigure(0, weight=1)
        
        return self.frame_main
    
    def create_frame_loading_text(self):
        
        self.frame_loading_text = ttk.Frame(self.frame_main)
        
        self.lbl_loading_text = ttk.Label(self.frame_loading_text,
                                          text=self.loading_text)
        
        self.lbl_loading_text.pack(pady=2)
        
        return self.frame_loading_text
        
    def set_progress(self, progress_receipt):
        """
        This gets called when a queue is sent from a secondary thread
        to the main GUI thread.
        """
        
        # If the progress percentage has the string 'close', then that means
        # an error ocurred when attempting to download data and we should 
        # close the loading window.
        if progress_receipt.percentage == "close":
            self.destroy()
        else:
            print("Download %", progress_receipt.percentage)
            self.v_progress.set(progress_receipt.percentage)
            
            # If the progress is at 100%, close the window 500ms later 
            # automatically so that the user gets a chance to see the 
            # loading window's text.
            if progress_receipt.percentage == 100:
                current_text = self.lbl_loading_text["text"]
                self.lbl_loading_text.configure(text=current_text + "OK")
                self.after(500, self.destroy)
    
        
    def create_frame_progress(self):
        
        self.frame_progress = ttk.Frame(self.frame_main)
        
        self.v_progress = IntVar()
        
        self.progress_bar = ttk.Progressbar(self.frame_progress,
                                            mode=self.mode,
                                            length=350,
                                            variable=self.v_progress)
        

        if self.mode == "determinate":
            self.progress_bar.configure(maximum=100)
        else:
            self.progress_bar.start(interval=30)
        
        self.progress_bar.pack()
        
        return self.frame_progress
    


if __name__ == "__main__":
        
    root = LoadingWindow()
    LoadingWindow(root)
    
    root.mainloop()
