#=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~=~
#                                      numgeo - GiD interface
#                       Copyright (C) 2023-today Jan Machacek, Rahul Dogra
#=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~=~
#
# History
# - 14-12-2025, R. Dogra - Added Dimension Wizard
#=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~~=~=~

namespace eval ::numgeo::wizard {
    variable dim "2D (plane-strain)"
}

proc ::numgeo::wizard::IsDimensionAlreadySet {} {
    # Read current value from the customlib DOM
    if {![info exists ::gid_groups_conds::doc]} { return 0 }
    set doc $::gid_groups_conds::doc

    # value node:
    set xp {string(//container[@n='_dimensions_units_']/value[@n='_dimensions_']/@v)}
    set v ""
    catch { set v [$doc selectNodes $xp] }

    # If it has a non-empty value, we consider it already set
    if {[string trim $v] ne ""} { return 1 }
    return 0
}

#proc ::numgeo::wizard::_wait_splash_and_start {dir} {
#
#    # If already set in the model, do NOT ask again
#    if {[::numgeo::wizard::IsDimensionAlreadySet]} {
#        return
#    }
#
#    # (your existing checks)
#    if {[grab current] ne ""} {
#        after 100 [list ::numgeo::wizard::_wait_splash_and_start $dir]
#        return
#    }
#
#    set extra 0
#    foreach w [winfo children .] {
#        if {![winfo exists $w]} { continue }
#        if {[winfo toplevel $w] ne $w} { continue }
#        if {$w in {.gid .tkcon .#BWidget}} { continue }
#        set extra 1
#        break
#    }
#    if {$extra} {
#        after 100 [list ::numgeo::wizard::_wait_splash_and_start $dir]
#        return
#    }
#
#    after 50 { catch {::numgeo::wizard::Start} }
#}



proc ::numgeo::wizard::Start {} {
    variable dim

    catch {package require Tk}
    catch {package require ttk}

    set w .numgeo_dimension_wizard
    if {[winfo exists $w]} { destroy $w }

    toplevel $w
    wm title $w "numgeo - Dimension"
    wm resizable $w 0 0
    wm transient $w .gid

    ttk::frame $w.f -padding 12
    pack $w.f -fill both -expand 1

    ttk::label $w.f.l -text "Select the spatial dimension of the problem:"
    pack $w.f.l -anchor w -pady {0 8}

    ttk::radiobutton $w.f.r1 -text "2D - Plane strain" -variable ::numgeo::wizard::dim -value "2D (plane-strain)"
    ttk::radiobutton $w.f.r2 -text "2D - Axisymmetric" -variable ::numgeo::wizard::dim -value "2D: Axisymmetric"
    ttk::radiobutton $w.f.r3 -text "3D" -variable ::numgeo::wizard::dim -value "3D"
    pack $w.f.r1 $w.f.r2 $w.f.r3 -anchor w -pady 2

    ttk::separator $w.f.sep
    pack $w.f.sep -fill x -pady 10

    ttk::frame $w.f.btns
    pack $w.f.btns -fill x

    ttk::button $w.f.btns.ok -text "OK" -command [list ::numgeo::wizard::OnOK $w]
    ttk::button $w.f.btns.cancel -text "Cancel" -command [list ::numgeo::wizard::OnCancel $w]
    pack $w.f.btns.cancel -side right
    pack $w.f.btns.ok -side right -padx 8

    # force visible on top
    wm deiconify $w
    raise $w
    focus -force $w
    wm geometry $w +200+200
    wm attributes $w -topmost 1
    after 500 [list catch [list wm attributes $w -topmost 0]]

    grab $w
}

proc ::numgeo::wizard::OnCancel {w} {
    catch {grab release $w}
    if {[winfo exists $w]} { destroy $w }
}

proc ::numgeo::wizard::OnOK {w} {
    variable dim
    ::numgeo::wizard::SetDimensionInTree $dim
    catch {grab release $w}
    if {[winfo exists $w]} { destroy $w }
}

proc ::numgeo::wizard::SetDimensionInTree {dimString} {

    if {![info exists ::gid_groups_conds::doc]} {
        WarnWinText "Data tree DOM not ready."
        return
    }
    set doc $::gid_groups_conds::doc
    set n [$doc selectNodes {//container[@n='_dimensions_units_']/value[@n='_dimensions_']}]

    if {$n eq ""} {
        WarnWinText "Could not find _dimensions_ node in DOM."
        return
    }

    # Set value directly in DOM
    catch {$n setAttribute v $dimString}

    # Ensure GiD considers project modified so it is saved
    catch {::gid_groups_conds::set_modified_flag 1}

    # Refresh UI
    catch {::gid_groups_conds::update_data_tree}
    catch {::gid_groups_conds::actualize_conditions_window}
}
