#!/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.4
set COPYRIGHT "Copyright (C) 2021-2025 Claude Rubinson"
set NAME Fiss
set DESCR "Fiss Chart Generator"
set DETAIL "Produce publication-quality Fiss configuration charts."

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

# === 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.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]
    .t delete 1.0 end
    .t insert end [readFile $f]
    wm title . "Fiss Chart - $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 - $f"
}

$m.file add command -label "Save As" -underline 5 -command {
    set f [tk_getSaveFile -defaultextension .fiss]
    wm title . "Fiss Chart - $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 .}

# help menu
menu $m.help
$m.help add command -label "Docs" -command {}

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

wm title . "Fiss Chart"

text .t -wrap none -width 80 -height 30 -background white -font {TkFixedFont 14} -yscrollcommand ".ys set" -xscrollcommand ".xs set"
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
    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 launchViewer {text} {
    set tmp [file tempfile]
    set ch [open $tmp w]
    puts $ch $text
    close $ch

    # use same interpreter (wish, starpack, etc.) to launch viewer
    set childScript [file join [file dirname [info script]] fiss-viewer.tcl]
    set cmd [list [info nameofexecutable] $childScript -y $tmp --del-tmp-cfg]

    catch {exec {*}$cmd &} err
}

proc launchWizard {} {
    # use same interpreter (wish, starpack, etc.) to launch viewer
    set childScript [file join [file dirname [info script]] fiss-wizard.tcl]
    set cmd [list [info nameofexecutable] $childScript]

    .t insert 1.0 [exec {*}$cmd]
}
