import tkinter as tk
from tkinter import messagebox

class RoundSetupDialog:
    def __init__(self, parent, callback):
        self.top = tk.Toplevel(parent)
        #self.decorator = HackerWindowDecorator(self.top)
        self.callback = callback
        
        # Hacker style configuration
        self.top.title("Round Setup")
        self.top.geometry("300x200")
        self.top.config(bg="black", bd=2, relief=tk.SOLID)
        #self.top.attributes("-topmost", True) :: Already in decorator
        self.top.configure(highlightthickness=2, highlightcolor="#00FF00", highlightbackground="#00FF00")
        self.top.transient(parent)
        self.top.grab_set()
        
        self.center_window()
        
        # Headers and labels in green
        tk.Label(
            self.top,
            text="Enter Starting Rounds",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 14, "bold")
        ).pack(pady=10)
        
        # Live rounds frame
        live_frame = tk.Frame(self.top, bg="black")
        live_frame.pack(pady=5)
        
        tk.Label(
            live_frame,
            text="Live Rounds:",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12)
        ).pack(side="left", padx=5)
        
        self.live_entry = tk.Entry(
            live_frame, 
            fg="#00FF00", 
            bg="black", 
            insertbackground="#00FF00",
            font=("Px437 IBM Model30r0", 12), 
            width=5,
            bd=1, 
            relief=tk.SOLID,
            highlightthickness=1, 
            highlightcolor="#00FF00", 
            highlightbackground="#00FF00"
        )
        self.live_entry.pack(side="left", padx=5)
        
        # Blanc rounds frame
        blanc_frame = tk.Frame(self.top, bg="black")
        blanc_frame.pack(pady=5)
        
        tk.Label(
            blanc_frame,
            text="Blanc Rounds:",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12)
        ).pack(side="left", padx=5)
        
        self.blanc_entry = tk.Entry(
            blanc_frame, 
            fg="#00FF00", 
            bg="black", 
            insertbackground="#00FF00",
            font=("Px437 IBM Model30r0", 12), 
            width=5,
            bd=1, 
            relief=tk.SOLID,
            highlightthickness=1, 
            highlightcolor="#00FF00", 
            highlightbackground="#00FF00"
        )
        self.blanc_entry.pack(side="left", padx=5)
        
        # Enter button with hacker style
        tk.Button(
            self.top,
            text="Start Game",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12),
            activebackground="#004400",
            activeforeground="#00FF00",
            bd=1,
            relief=tk.SOLID,
            command=self.on_submit
        ).pack(pady=20)
    
    def center_window(self):
        self.top.update_idletasks()
        width = self.top.winfo_width()
        height = self.top.winfo_height()
        x = (self.top.winfo_screenwidth() // 2) - (width // 2)
        y = (self.top.winfo_screenheight() // 2) - (height // 2)
        self.top.geometry(f'+{x}+{y}')
    
    def on_submit(self):
        try:
            live = int(self.live_entry.get())
            blanc = int(self.blanc_entry.get())
            
            if live < 0 or blanc < 0:
                messagebox.showerror("Error", "Please enter positive numbers", background="black", foreground="#00FF00")
                return
            
            self.callback(live, blanc)
            self.top.destroy()
            
        except ValueError:
            messagebox.showerror("Error", "Please enter valid numbers", background="black", foreground="#00FF00")

class BuckshotHelper:
    def __init__(self):
        # Initialize game state
        self.round_number = 1
        self.live_rounds = 0
        self.blanc_rounds = 0
        self.selected_type = None
        self.history = []
        
        # Create main window with hacker style
        self.root = tk.Tk()
        # Right after self.root = tk.Tk()
        self.decorator = HackerWindowDecorator(self.root)
        self.root.title("Buckshot Roulette Helper")
        self.root.geometry("400x600")
        self.root.config(bg="black")
        #self.root.attributes("-topmost", True) :: Already in decorator
        self.root.configure(
            highlightthickness=2, 
            highlightcolor="#00FF00", 
            highlightbackground="#00FF00"
        )
        
        self.setup_ui()
        self.show_round_setup()
        
    def setup_ui(self):
        # Hacker style header
        self.create_header("Tracking", 16)
        
        # Stats Frame
        stats_frame = tk.Frame(self.root, bg="black")
        stats_frame.pack(pady=5)
        
        self.round_label = self.create_stat_label(stats_frame, f"Round: {self.round_number}")
        self.live_label = self.create_stat_label(stats_frame, f"Live: {self.live_rounds}")
        self.blanc_label = self.create_stat_label(stats_frame, f"Blanc: {self.blanc_rounds}")
        
        # Probability Display
        self.prob_label = tk.Label(
            self.root,
            text="Probability of Live Round: 50.0%",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12),
            bd=1,
            relief=tk.SOLID
        )
        self.prob_label.pack(pady=10)
        
        # Telephone Section
        self.create_header("Telephone", 14)
        
        # Input Frame
        input_frame = tk.Frame(self.root, bg="black")
        input_frame.pack(pady=10)
        
        # Round Count Input
        self.round_input = tk.Entry(
            input_frame, 
            fg="#00FF00", 
            bg="black", 
            insertbackground="#00FF00",
            font=("Px437 IBM Model30r0", 12), 
            width=5,
            bd=1, 
            relief=tk.SOLID,
            highlightthickness=1, 
            highlightcolor="#00FF00", 
            highlightbackground="#00FF00"
        )
        self.round_input.pack(side="left", padx=5)
        
        # Type Selection Buttons
        self.live_button = tk.Button(
            input_frame,
            text="Live",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12),
            activebackground="#004400",
            activeforeground="#00FF00",
            bd=1,
            relief=tk.SOLID,
            command=self.select_live
        )
        self.live_button.pack(side="left", padx=5)
        
        self.blanc_button = tk.Button(
            input_frame,
            text="Blanc",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12),
            activebackground="#004400",
            activeforeground="#00FF00",
            bd=1,
            relief=tk.SOLID,
            command=self.select_blanc
        )
        self.blanc_button.pack(side="left", padx=5)
        
        # Enter Button
        tk.Button(
            input_frame,
            text="Enter",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12),
            activebackground="#004400",
            activeforeground="#00FF00",
            bd=1,
            relief=tk.SOLID,
            command=self.handle_telephone
        ).pack(side="left", padx=5)
        
        # History Frame
        history_frame = tk.Frame(self.root, bg="black")
        history_frame.pack(pady=10, fill="both", expand=True)
        
        # History Label
        tk.Label(
            history_frame,
            text="Telephone History",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12, "bold"),
            bd=1,
            relief=tk.SOLID
        ).pack(pady=5)
        
        # Create a frame for history entries
        self.history_entries_frame = tk.Frame(history_frame, bg="black")
        self.history_entries_frame.pack(fill="both", expand=True)
        
        # Ejected Rounds Section
        self.create_header("Ejected Rounds", 14)
        
        ejected_frame = tk.Frame(self.root, bg="black")
        ejected_frame.pack(pady=10)
        
        # Ejected Buttons
        tk.Button(
            ejected_frame,
            text="Live Round Ejected",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12),
            activebackground="#004400",
            activeforeground="#00FF00",
            bd=1,
            relief=tk.SOLID,
            command=self.live_round_ejected
        ).pack(side="left", padx=5)
        
        tk.Button(
            ejected_frame,
            text="Blanc Round Ejected",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12),
            activebackground="#004400",
            activeforeground="#00FF00",
            bd=1,
            relief=tk.SOLID,
            command=self.blanc_round_ejected
        ).pack(side="left", padx=5)
        
        # Buttons Frame
        buttons_frame = tk.Frame(self.root, bg="black")
        buttons_frame.pack(pady=10)
        
        # Reset Button
        tk.Button(
            buttons_frame,
            text="Reset Game",
            fg="#FF0000",
            bg="black",
            font=("Px437 IBM Model30r0", 12),
            activebackground="#440000",
            activeforeground="#FF0000",
            bd=1,
            relief=tk.SOLID,
            command=self.reset_game
        ).pack(side="left", padx=10)
        
        # New Round Button
        tk.Button(
            buttons_frame,
            text="New Round",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12),
            activebackground="#004400",
            activeforeground="#00FF00",
            bd=1,
            relief=tk.SOLID,
            command=self.show_round_setup
        ).pack(side="left", padx=10)
    
    def create_header(self, text, size):
        tk.Label(
            self.root,
            text=text,
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", size, "bold"),
            bd=1,
            relief=tk.SOLID
        ).pack(pady=10)
    
    def create_stat_label(self, parent, text):
        label = tk.Label(
            parent,
            text=text,
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 12),
            width=12,
            bd=1,
            relief=tk.SOLID
        )
        label.pack(side="left", padx=5)
        return label
    
    def select_live(self):
        self.selected_type = "live"
        self.live_button.config(bg="#004400")
        self.blanc_button.config(bg="black")
    
    def select_blanc(self):
        self.selected_type = "blanc"
        self.blanc_button.config(bg="#004400")
        self.live_button.config(bg="black")
    
    def update_probability(self):
        total_rounds = self.live_rounds + self.blanc_rounds
        if total_rounds > 0:
            live_prob = (self.live_rounds / total_rounds) * 100
            self.prob_label.config(text=f"Probability of Live Round: {live_prob:.1f}%")
        else:
            self.prob_label.config(text="No rounds remaining")
    
    def add_history_entry(self, count, round_type):
        entry = tk.Label(
            self.history_entries_frame,
            text=f"{count} {round_type.capitalize()}",
            fg="#00FF00",
            bg="black",
            font=("Px437 IBM Model30r0", 10),
            width=20,
            bd=1,
            relief=tk.SOLID
        )
        entry.pack(pady=2)
        
        self.history.append((count, round_type))
    
    def handle_telephone(self):
        try:
            count = int(self.round_input.get())
            if count <= 0:
                messagebox.showerror("Error", "Please enter a positive number", background="black", foreground="#00FF00")
                return
                
            if self.selected_type == "live":
                self.live_rounds += count
                self.add_history_entry(count, "live")
            elif self.selected_type == "blanc":
                self.blanc_rounds += count
                self.add_history_entry(count, "blanc")
            else:
                messagebox.showerror("Error", "Please select round type", background="black", foreground="#00FF00")
                return
                
            self.update_labels()
            self.round_input.delete(0, tk.END)
            
        except ValueError:
            messagebox.showerror("Error", "Please enter a valid number", background="black", foreground="#00FF00")
    
    def live_round_ejected(self):
        if self.live_rounds > 0:
            self.live_rounds -= 1
            self.round_number += 1
            self.update_labels()
        else:
            messagebox.showwarning("Warning", "No live rounds remaining", background="black", foreground="#00FF00")
    
    def blanc_round_ejected(self):
        if self.blanc_rounds > 0:
            self.blanc_rounds -= 1
            self.round_number += 1
            self.update_labels()
        else:
            messagebox.showwarning("Warning", "No blanc rounds remaining", background="black", foreground="#00FF00")
    
    def update_labels(self):
        self.round_label.config(text=f"Round: {self.round_number}")
        self.live_label.config(text=f"Live: {self.live_rounds}")
        self.blanc_label.config(text=f"Blanc: {self.blanc_rounds}")
        self.update_probability()
    
    def reset_game(self):
        self.round_number = 1
        self.update_labels()
        
        # Clear history
        for widget in self.history_entries_frame.winfo_children():
            widget.destroy()
        self.history = []
        
        messagebox.showinfo("Game Reset", "Game has been reset to initial state", background="black", foreground="#00FF00")
    
    def show_round_setup(self):
        RoundSetupDialog(self.root, self.start_new_round)
    
    def start_new_round(self, live, blanc):
        self.round_number = 1
        self.live_rounds = live
        self.blanc_rounds = blanc
        
        # Clear history
        for widget in self.history_entries_frame.winfo_children():
            widget.destroy()
        self.history = []
        
        self.update_labels()
    
    def run(self):
        self.root.mainloop()

import tkinter as tk

class HackerWindowDecorator:
    def __init__(self, root):
        self.root = root
        self.root.overrideredirect(True)  # Remove default window decorations
        self.root.configure(bg='black')
        self.root.attributes("-topmost", True)
        
        # Titlebar
        self.titlebar = tk.Frame(self.root, bg='black', height=30, bd=1, relief=tk.SOLID)
        self.titlebar.pack(fill=tk.X, side=tk.TOP)
        self.titlebar.pack_propagate(False)
        
        # Title Label
        self.title_label = tk.Label(
            self.titlebar, 
            text="Buckshot Roulette Helper", 
            fg='#00FF00', 
            bg='black', 
            font=('Px437 IBM Model30r0', 12)
        )
        self.title_label.pack(side=tk.LEFT, padx=10)
        
        # Close Button
        self.close_button = tk.Button(
            self.titlebar, 
            text='X', 
            fg='#FF0000', 
            bg='black', 
            font=('Px437 IBM Model30r0', 12), 
            bd=1, 
            relief=tk.SOLID,
            command=self.root.quit
        )
        self.close_button.pack(side=tk.RIGHT, padx=5)
        
        # Make window movable
        self.titlebar.bind('<Button-1>', self.start_move)
        self.titlebar.bind('<ButtonRelease-1>', self.stop_move)
        self.titlebar.bind('<B1-Motion>', self.do_move)
        
        # Green border
        self.root.configure(
            highlightthickness=2, 
            highlightcolor="#00FF00", 
            highlightbackground="#00FF00"
        )
    
    def start_move(self, event):
        self.x = event.x
        self.y = event.y
    
    def stop_move(self, event):
        self.x = None
        self.y = None
    
    def do_move(self, event):
        deltax = event.x - self.x
        deltay = event.y - self.y
        x = self.root.winfo_x() + deltax
        y = self.root.winfo_y() + deltay
        self.root.geometry(f"+{x}+{y}")
                         
if __name__ == "__main__":
    app = BuckshotHelper()
    app.run()