# http_file_import_dialog.py
#
# Copyright 2025 Pavel Baksy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later

from gi.repository import Adw, Gtk, GObject, Gio
from typing import List

from .http_file_importer import (
    parse_http_file, build_http_request,
    HttpFileParseResult, HttpFileRequest,
)

_METHOD_CLASS = {
    'GET':    'accent',
    'POST':   'success',
    'PUT':    'warning',
    'PATCH':  'warning',
    'DELETE': 'error',
}


class HttpFileImportDialog(Adw.Dialog):
    """Two-step dialog: pick .http file → select requests → import."""

    __gtype_name__ = 'HttpFileImportDialog'

    __gsignals__ = {
        'import-completed': (GObject.SIGNAL_RUN_FIRST, None, (str,)),
    }

    def __init__(self, project_manager, existing_projects):
        super().__init__()
        self.set_title("Import from .http File")
        self.set_content_width(580)
        self.set_content_height(560)

        self._pm = project_manager
        self._existing_projects = list(existing_projects)
        self._parse_result: HttpFileParseResult | None = None
        self._req_checkboxes: List[tuple[HttpFileRequest, Gtk.CheckButton]] = []

        self._build_ui()

    # ------------------------------------------------------------------ build

    def _build_ui(self):
        tv = Adw.ToolbarView()
        tv.add_top_bar(Adw.HeaderBar())

        self._stack = Gtk.Stack()
        self._stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
        self._stack.set_transition_duration(200)
        self._stack.add_named(self._make_file_page(), "file")
        self._stack.add_named(self._make_reqs_page(), "reqs")
        tv.set_content(self._stack)

        ab = Gtk.ActionBar()

        self._back_btn = Gtk.Button(label="Back")
        self._back_btn.connect("clicked", lambda _: self._show_file_page())
        ab.pack_start(self._back_btn)

        self._browse_btn = Gtk.Button(label="Choose File…")
        self._browse_btn.add_css_class("suggested-action")
        self._browse_btn.connect("clicked", self._on_browse)
        ab.pack_end(self._browse_btn)

        self._import_btn = Gtk.Button(label="Import")
        self._import_btn.add_css_class("suggested-action")
        self._import_btn.connect("clicked", self._on_import)
        ab.pack_end(self._import_btn)

        tv.add_bottom_bar(ab)
        self.set_child(tv)
        self._show_file_page()

    def _make_file_page(self) -> Gtk.Widget:
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=16)
        box.set_valign(Gtk.Align.CENTER)
        box.set_vexpand(True)
        box.set_margin_top(32)
        box.set_margin_bottom(24)
        box.set_margin_start(24)
        box.set_margin_end(24)

        icon = Gtk.Image.new_from_icon_name("document-open-symbolic")
        icon.set_pixel_size(64)
        icon.add_css_class("dim-label")
        box.append(icon)

        title = Gtk.Label(label="Import from .http File")
        title.add_css_class("title-2")
        box.append(title)

        subtitle = Gtk.Label(
            label="Choose a .http or .rest file (IntelliJ HTTP Client format) to import its requests"
        )
        subtitle.add_css_class("dim-label")
        subtitle.set_wrap(True)
        subtitle.set_justify(Gtk.Justification.CENTER)
        box.append(subtitle)

        self._file_label = Gtk.Label()
        self._file_label.add_css_class("monospace")
        self._file_label.add_css_class("dim-label")
        self._file_label.set_visible(False)
        box.append(self._file_label)

        self._err_label = Gtk.Label()
        self._err_label.add_css_class("error")
        self._err_label.set_wrap(True)
        self._err_label.set_visible(False)
        box.append(self._err_label)

        return box

    def _make_reqs_page(self) -> Gtk.Widget:
        outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        outer.set_margin_top(14)
        outer.set_margin_bottom(14)
        outer.set_margin_start(16)
        outer.set_margin_end(16)

        # File info row
        file_grp = Adw.PreferencesGroup()
        file_grp.set_title("File")
        self._file_row = Adw.ActionRow()
        self._file_row.set_title("Path")
        file_grp.add(self._file_row)
        outer.append(file_grp)

        # Requests header with select-all button
        reqs_hdr = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=8)
        reqs_hdr.set_margin_top(2)
        reqs_lbl = Gtk.Label(label="Requests")
        reqs_lbl.add_css_class("title-4")
        reqs_lbl.set_hexpand(True)
        reqs_lbl.set_xalign(0)
        reqs_hdr.append(reqs_lbl)
        self._sel_all_btn = Gtk.Button(label="Deselect All")
        self._sel_all_btn.add_css_class("flat")
        self._sel_all_btn.connect("clicked", self._on_select_all)
        reqs_hdr.append(self._sel_all_btn)
        outer.append(reqs_hdr)

        scroll = Gtk.ScrolledWindow()
        scroll.set_vexpand(True)
        scroll.set_min_content_height(80)
        self._reqs_list = Gtk.ListBox()
        self._reqs_list.add_css_class("boxed-list")
        self._reqs_list.set_selection_mode(Gtk.SelectionMode.NONE)
        scroll.set_child(self._reqs_list)
        outer.append(scroll)

        # Import target
        tgt_grp = Adw.PreferencesGroup()
        tgt_grp.set_title("Import to")
        tgt_grp.set_margin_top(2)

        new_row = Adw.ActionRow()
        new_row.set_title("Create new project")
        self._new_radio = Gtk.CheckButton()
        self._new_radio.set_active(True)
        new_row.add_prefix(self._new_radio)
        new_row.set_activatable_widget(self._new_radio)
        self._new_name_entry = Gtk.Entry()
        self._new_name_entry.set_placeholder_text("Project name")
        self._new_name_entry.set_hexpand(True)
        self._new_name_entry.set_valign(Gtk.Align.CENTER)
        new_row.add_suffix(self._new_name_entry)
        tgt_grp.add(new_row)

        if self._existing_projects:
            exist_row = Adw.ActionRow()
            exist_row.set_title("Add to existing project")
            self._exist_radio = Gtk.CheckButton()
            self._exist_radio.set_group(self._new_radio)
            exist_row.add_prefix(self._exist_radio)
            exist_row.set_activatable_widget(self._exist_radio)
            proj_list = Gtk.StringList.new([p.name for p in self._existing_projects])
            self._proj_dd = Gtk.DropDown(model=proj_list)
            self._proj_dd.set_valign(Gtk.Align.CENTER)
            self._proj_dd.set_sensitive(False)
            exist_row.add_suffix(self._proj_dd)
            tgt_grp.add(exist_row)
            self._new_radio.connect("toggled", self._on_target_toggled)
        else:
            self._exist_radio = None
            self._proj_dd = None

        outer.append(tgt_grp)
        return outer

    # ----------------------------------------------------------- page control

    def _show_file_page(self):
        self._stack.set_visible_child_name("file")
        self._back_btn.set_visible(False)
        self._browse_btn.set_visible(True)
        self._import_btn.set_visible(False)

    def _show_reqs_page(self):
        self._stack.set_visible_child_name("reqs")
        self._back_btn.set_visible(True)
        self._browse_btn.set_visible(False)
        self._import_btn.set_visible(True)

    # --------------------------------------------------------- event handlers

    def _on_target_toggled(self, _radio):
        is_new = self._new_radio.get_active()
        self._new_name_entry.set_sensitive(is_new)
        if self._proj_dd:
            self._proj_dd.set_sensitive(not is_new)

    def _on_select_all(self, _btn):
        all_on = all(cb.get_active() for _, cb in self._req_checkboxes)
        new_state = not all_on
        for _, cb in self._req_checkboxes:
            cb.set_active(new_state)
        self._sel_all_btn.set_label("Deselect All" if new_state else "Select All")

    def _on_browse(self, _btn):
        self._err_label.set_visible(False)

        chooser = Gtk.FileChooserNative(
            title="Open HTTP File",
            action=Gtk.FileChooserAction.OPEN,
            accept_label="Open",
            cancel_label="Cancel",
            transient_for=self.get_root(),
        )

        http_filter = Gtk.FileFilter()
        http_filter.set_name("HTTP files (*.http, *.rest)")
        http_filter.add_pattern("*.http")
        http_filter.add_pattern("*.rest")
        chooser.add_filter(http_filter)

        all_filter = Gtk.FileFilter()
        all_filter.set_name("All files")
        all_filter.add_pattern("*")
        chooser.add_filter(all_filter)

        chooser.connect("response", self._on_file_chosen)
        chooser.show()
        # Keep a reference so GC doesn't collect it
        self._chooser = chooser

    def _on_file_chosen(self, chooser, response):
        if response != Gtk.ResponseType.ACCEPT:
            return

        gfile = chooser.get_file()
        if not gfile:
            return

        path = gfile.get_path()

        try:
            content = gfile.load_contents(None)[1].decode('utf-8', errors='replace')
        except Exception as e:
            self._set_err(f"Could not read file: {e}")
            return

        parsed = parse_http_file(content)

        if parsed.error:
            self._set_err(parsed.error)
            self._file_label.set_label(path or "")
            self._file_label.set_visible(True)
            return

        self._parse_result = parsed
        self._current_path = path or ""
        self._populate_reqs_page(parsed, path or "")
        self._show_reqs_page()

    def _on_import(self, _btn):
        if not self._parse_result:
            return

        selected = [req for req, cb in self._req_checkboxes if cb.get_active()]
        if not selected:
            return

        if self._new_radio.get_active():
            name = self._new_name_entry.get_text().strip()
            if not name:
                return
            project = self._pm.add_project(name)
            project_id = project.id
        elif self._exist_radio and self._exist_radio.get_active() and self._proj_dd:
            idx = self._proj_dd.get_selected()
            if idx >= len(self._existing_projects):
                return
            project_id = self._existing_projects[idx].id
        else:
            return

        for req in selected:
            http_req = build_http_request(req)
            self._pm.add_request(project_id, req.name, http_req)

        self.emit('import-completed', project_id)
        self.close()

    # ----------------------------------------------------------------- helpers

    def _set_err(self, msg: str):
        self._err_label.set_text(msg)
        self._err_label.set_visible(True)

    def _populate_reqs_page(self, result: HttpFileParseResult, path: str):
        self._file_row.set_subtitle(path)

        while child := self._reqs_list.get_first_child():
            self._reqs_list.remove(child)
        self._req_checkboxes.clear()

        for req in result.requests:
            row = Adw.ActionRow()
            row.set_title(req.name)
            row.set_subtitle(f"{req.method} {req.url}")

            badge = Gtk.Label(label=req.method)
            badge.add_css_class("caption")
            badge.add_css_class("monospace")
            badge.add_css_class(_METHOD_CLASS.get(req.method, 'dim-label'))
            badge.set_valign(Gtk.Align.CENTER)
            row.add_suffix(badge)

            cb = Gtk.CheckButton()
            cb.set_active(True)
            row.add_prefix(cb)
            row.set_activatable_widget(cb)

            self._reqs_list.append(row)
            self._req_checkboxes.append((req, cb))

        # Pre-fill project name from filename (stem of the path)
        import os
        stem = os.path.splitext(os.path.basename(path))[0]
        self._new_name_entry.set_text(stem)
        self._sel_all_btn.set_label("Deselect All")
