#!/usr/bin/env tclsh

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

package require Tk

# === common code shared with other fiss apps; move to shared lib
set VERSION 0.6.1
set COPYRIGHT "Copyright (C) 2021-2026 Claude Rubinson"
set NAME Fiss
set DESCR "Fiss Chart Generator"
set DETAIL "Produce publication-quality Fiss configuration charts."

set AGPL30 "This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero 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 Affero General Public License for more details.

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

proc about [] {
    global VERSION
    global COPYRIGHT
    global NAME
    global DESCR
    global DETAIL
    global AGPL30
    tk_messageBox -title "About $NAME" -message "$DESCR" -detail "$DETAIL\n\nVersion $VERSION\n$COPYRIGHT\n\n$AGPL30"
}

# === end common code? ===

# main menu
option add *tearOff 0
menu .menubar
. configure -menu .menubar
set m .menubar
$m add cascade -menu $m.file -label File -underline 0
$m add cascade -menu $m.edit -label Edit -underline 0
$m add cascade -menu $m.help -label Help

# file menu
menu $m.file

$m.file add command -label "New" -underline 0 -command {
    .t delete 1.0 end
    launchWizard
}

$m.file add command -label "Open" -underline 0 -command {
    set f [tk_getOpenFile]
    if {$f eq ""} {
	return
    }
    .t delete 1.0 end
    .t insert end [readFile $f]
    wm title . "Fiss Chart Editor - $f"
}

$m.file add command -label "Save" -underline 0 -command {
    if {[info exists f]} {
	copyTextToFile $f
    } {
	set f [tk_getSaveFile -defaultextension .fiss]
	copyTextToFile $f
    }	
    wm title . "Fiss Chart Editor - $f"
}

$m.file add command -label "Save As" -underline 5 -command {
    set f [tk_getSaveFile -defaultextension .fiss]
    wm title . "Fiss Chart Editor - $f"
}

$m.file add separator

$m.file add command -label "Preview Chart" -underline 3 -command {
    launchViewer [.t get 1.0 end]
}

$m.file add separator
$m.file add command -label "Quit" -underline 0 -command {destroy .}

# edit menu
menu $m.edit
$m.edit add command -label "Undo" -command {undoText .t}
$m.edit add command -label "Redo" -command {redoText .t}
$m.edit add separator
$m.edit add command -label "Cut" -command {event generate .t <<Cut>>}
$m.edit add command -label "Copy" -command {event generate .t <<Copy>>}
$m.edit add command -label "Paste" -command {event generate .t <<Paste>>}
$m.edit add separator
$m.edit add command -label "Select all" -command {.t tag add sel 1.0 end}

# help menu
menu $m.help
$m.help add command -label "Manual" -command {openURL "MANUAL.html"}
$m.help add command -label "License" -command {openURL "COPYING.html"}

proc openURL url {
    switch -- $::tcl_platform(os) {
        "Windows NT" {
            exec {*}[auto_execok start] "" $url &
        }
        "Darwin" {
            exec open $url &
        }
        default {
            exec xdg-open $url &
        }
    }
}

# should probably create/find a reusable widget for the About dialog
$m.help add command -label "About" -command {about}

wm title . "Fiss Chart Editor"

text .t -wrap none -width 80 -height 30 -background white -font {TkFixedFont 14} -yscrollcommand ".ys set" -xscrollcommand ".xs set" -undo 1
ttk::scrollbar .ys -orient vertical -command ".t yview"
ttk::scrollbar .xs -orient horizontal -command ".t xview"

grid .t -sticky nwes -column 0 -row 0
grid .ys -sticky ns -column 1 -row 0
grid .xs -sticky ew -column 0 -row 1
grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1

proc readFile fname {
    set ch [open $fname r]
    set out [read -nonewline $ch]
    close $ch
    .t edit modified 0
    return $out
}

proc copyTextToFile fname {
    set text [.t get 1.0 end]

    # write text to temp file
    set tmp [file tempfile]
    set ch [open $tmp w]
    puts -nonewline $ch $text
    close $ch

    # move temp file onto save file
    file rename -force $tmp $fname
}

proc undoText tw {
    catch {$tw edit undo}
}

proc redoText tw {
    catch {$tw edit redo}
}

proc launchViewer {text} {
    set tmp [file tempfile]
    set ch [open $tmp w]
    puts $ch $text
    close $ch

    set child [file join [file dirname "$::argv0"] fiss-viewer.tcl]
    set cmd [list [info nameofexecutable] $child -y $tmp --ephemeral-config]
    exec {*}$cmd
}

proc launchWizard {} {
    set child [file join [file dirname "$::argv0"] fiss-wizard.tcl]
    set cmd [list [info nameofexecutable] $child]
    .t insert 1.0 [exec {*}$cmd]
}
