"""
InstructionsDialog - диалог с инструкцией на английском (tkinter)
"""

import tkinter as tk
from tkinter import ttk


class InstructionsDialog(tk.Toplevel):
    """Диалог с подробной инструкцией пользователя на английском"""

    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        self.title("Instructions")
        self.geometry("750x600")
        self.minsize(700, 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="15")
        main_frame.pack(fill='both', expand=True)

        # Title
        ttk.Label(main_frame, text="Astro Session Watcher - User Guide", font=('Segoe UI', 16, 'bold')).pack(pady=(0, 10))

        # Text with scrollbar
        text_frame = ttk.Frame(main_frame)
        text_frame.pack(fill='both', expand=True)

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

        self.text_widget = tk.Text(text_frame, yscrollcommand=scrollbar.set, wrap='word',
                                    bg='#1e1e1e', fg='#e0e0e0', font=('Consolas', 10))
        self.text_widget.pack(fill='both', expand=True)
        scrollbar.config(command=self.text_widget.yview)

        # Instructions text
        instructions = """======================= ASTRO SESSION WATCHER =======================

The application automatically tracks new photos in the selected folder,
sorts them by observation targets and maintains detailed logs.

📸 WHAT IS IT FOR?
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

• Automatically distribute shots into target folders
• Keep a log of each session
• Don't miss a single frame when changing targets
• Store equipment and celestial bodies history

🚀 HOW IT WORKS?
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

1. Select the folder where your camera saves photos
2. The app creates a session folder: "AstroSession_YYYY-MM-DD"
3. Each target gets its own subfolder inside the session folder
4. When you change target (press "New Target"), all accumulated files are moved
5. When you end the session, remaining files are moved to the last target

📝 STEP-BY-STEP GUIDE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

█ 1. FIRST LAUNCH (SETUP)
───────────────────────────────────────────────────────────────────

• Go to "File" → "Equipment" and add your cameras and lenses/telescopes
• Go to "File" → "Celestial Bodies" and add your observation targets
• All data is saved automatically in config files

█ 2. STARTING A SESSION
───────────────────────────────────────────────────────────────────

1. Click "Browse" and select the folder where your camera saves photos
2. Select camera and lens/telescope from dropdowns
3. Enter target name (or select from celestial bodies list)
4. Click "▶ Start Tracking"

✅ After launch:
   • Status changes to "● ON AIR" with blinking
   • "New Target" button becomes active
   • Session folder "AstroSession_date" is created
   • Inside - folder with your first target

█ 3. CHANGING TARGET DURING SESSION
───────────────────────────────────────────────────────────────────

1. Click "New Target" button (or Ctrl+Shift+N)
2. Enter new target name
3. The app automatically moves all accumulated files to the previous target
4. Creates new folder for the next target
5. Resets file counter
6. Continues tracking

💡 IMPORTANT: If there are files in the watch folder before changing target,
          they will NOT be lost - all will be moved!

█ 4. ENDING A SESSION
───────────────────────────────────────────────────────────────────

1. Click "■ Stop" (or Ctrl+X)
2. The app moves all remaining files to the last target
3. Writes final session log
4. Shows dialog with option to open session folder
5. Restores interface for new session

⌨️ HOTKEYS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Ctrl + O       → Select watch folder
Ctrl + E       → Equipment management
Ctrl + B       → Celestial bodies management
Ctrl + S       → Start session
Ctrl + X       → Stop session
Ctrl + F       → Open current session folder
Ctrl + Shift+N → Create new target
F1             → About
F2             → This instruction

🔧 CONFIG FILES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

📄 astro_settings.json        ← cameras, lenses, last folder
📄 celestial_bodies.json      ← list of celestial bodies

All files are stored in the program folder. You can edit them manually.

📧 TECHNICAL SUPPORT
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Text me: norvicdev@gmail.com
Developer: Vic Sergeev
Version: 0.4.0-alpha
"""

        self.text_widget.insert('1.0', instructions)
        self.text_widget.config(state='disabled')

        # 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}')