"""
CelestialDialog - диалог управления небесными телами (tkinter)
"""

import tkinter as tk
from tkinter import ttk, messagebox


class CelestialDialog(tk.Toplevel):
    """Диалог для управления списком небесных тел"""

    def __init__(self, parent, config_service):
        super().__init__(parent)
        self.parent = parent
        self.config_service = config_service
        self.celestial_bodies = self.config_service.get_celestial_bodies()
        self.selected_body = None

        self.title("Celestial Bodies")
        self.geometry("500x550")
        self.minsize(450, 500)
        self.transient(parent)
        self.grab_set()

        self._create_ui()
        self._center_window()

    def _create_ui(self):
        # Main frame
        main_frame = ttk.Frame(self, padding="20")
        main_frame.pack(fill='both', expand=True)

        # Title
        ttk.Label(main_frame, text="Celestial Bodies", font=('Segoe UI', 14, 'bold')).pack(pady=(0, 10))
        ttk.Label(main_frame, text="List of observation targets", font=('Segoe UI', 10)).pack(pady=(0, 15))

        # Listbox with scrollbar
        list_frame = ttk.Frame(main_frame)
        list_frame.pack(fill='both', expand=True, pady=(0, 10))

        scrollbar = ttk.Scrollbar(list_frame)
        scrollbar.pack(side='right', fill='y')

        self.bodies_listbox = tk.Listbox(list_frame, yscrollcommand=scrollbar.set, height=15,
                                         bg='#2d2d2d', fg='#e0e0e0',
                                         selectbackground='#4CAF50', selectforeground='white',
                                         font=('Segoe UI', 10))
        self.bodies_listbox.pack(fill='both', expand=True)
        scrollbar.config(command=self.bodies_listbox.yview)

        for body in self.celestial_bodies:
            self.bodies_listbox.insert('end', body)

        self.bodies_listbox.bind('<<ListboxSelect>>', self._on_body_select)

        # Add new body
        add_frame = ttk.Frame(main_frame)
        add_frame.pack(fill='x', pady=(0, 10))

        self.new_body_entry = ttk.Entry(add_frame)
        self.new_body_entry.pack(side='left', fill='x', expand=True, padx=(0, 10))
        ttk.Button(add_frame, text="Add", command=self._add_celestial_body).pack(side='right')

        # Buttons
        btn_frame = ttk.Frame(main_frame)
        btn_frame.pack(pady=(0, 10))

        self.remove_btn = ttk.Button(btn_frame, text="Remove Selected", command=self._remove_celestial_body, state='disabled')
        self.remove_btn.pack(side='left', padx=5)

        self.edit_btn = ttk.Button(btn_frame, text="Edit Selected", command=self._edit_celestial_body, state='disabled')
        self.edit_btn.pack(side='left', padx=5)

        # Close button
        ttk.Button(main_frame, text="Close", command=self.destroy).pack(pady=10)

    def _center_window(self):
        self.update_idletasks()
        x = self.parent.winfo_x() + (self.parent.winfo_width() // 2) - (self.winfo_width() // 2)
        y = self.parent.winfo_y() + (self.parent.winfo_height() // 2) - (self.winfo_height() // 2)
        self.geometry(f'+{x}+{y}')

    def _on_body_select(self, event):
        selection = self.bodies_listbox.curselection()
        if selection:
            self.selected_body = self.bodies_listbox.get(selection[0])
            self.remove_btn.config(state='normal')
            self.edit_btn.config(state='normal')

    def _add_celestial_body(self):
        new_body = self.new_body_entry.get().strip()
        if not new_body:
            messagebox.showwarning("Warning", "Please enter object name!", parent=self)
            return

        if new_body in self.celestial_bodies:
            messagebox.showwarning("Warning", f"Object '{new_body}' already exists!", parent=self)
            return

        self.celestial_bodies.append(new_body)
        self.config_service.add_celestial_body(new_body)
        self.bodies_listbox.insert('end', new_body)
        self.new_body_entry.delete(0, 'end')
        messagebox.showinfo("Success", f"Object '{new_body}' added", parent=self)

    def _remove_celestial_body(self):
        if self.selected_body:
            reply = messagebox.askyesno("Remove Object", f"Remove '{self.selected_body}'?", parent=self)
            if reply:
                self.celestial_bodies.remove(self.selected_body)
                self.config_service.remove_celestial_body(self.selected_body)
                self.bodies_listbox.delete(0, 'end')
                for body in self.celestial_bodies:
                    self.bodies_listbox.insert('end', body)
                self.selected_body = None
                self.remove_btn.config(state='disabled')
                self.edit_btn.config(state='disabled')

    def _edit_celestial_body(self):
        if self.selected_body:
            dialog = tk.Toplevel(self)
            dialog.title("Edit Celestial Body")
            dialog.geometry("350x120")
            dialog.transient(self)
            dialog.grab_set()

            ttk.Label(dialog, text="New name:", font=('Segoe UI', 10)).pack(pady=15)
            entry = ttk.Entry(dialog, width=40)
            entry.insert(0, self.selected_body)
            entry.pack(pady=5)
            entry.focus()

            def save():
                new_name = entry.get().strip()
                if new_name and new_name != self.selected_body:
                    if new_name in self.celestial_bodies:
                        messagebox.showerror("Error", f"Object '{new_name}' already exists!", parent=dialog)
                        return

                    idx = self.celestial_bodies.index(self.selected_body)
                    self.celestial_bodies[idx] = new_name
                    self.config_service.update_celestial_body(self.selected_body, new_name)
                    self.bodies_listbox.delete(0, 'end')
                    for body in self.celestial_bodies:
                        self.bodies_listbox.insert('end', body)
                    dialog.destroy()
                elif new_name == self.selected_body:
                    dialog.destroy()
                else:
                    messagebox.showwarning("Warning", "Please enter a name!", parent=dialog)

            ttk.Button(dialog, text="Save", command=save).pack(pady=10)
            dialog.bind('<Return>', lambda e: save())