# -*- shell-script -*-

bashrun_window_id=""
bashrun_window_width=$COLUMNS
bashrun_window_height=$LINES
bashrun_window_mapped=1
bashrun_window_previous=0

function §window.id {
    
    if (( $# == 0 )); then
	printf '%s' "$bashrun_window_id"
    else
	bashrun_window_id="$1"
	§window.update
    fi
    return 0
}

function §window.mapped? { (( bashrun_window_mapped )); }
function §window.unmapped? { ! (( bashrun_window_mapped )); }

function §window.terminal? {
    [[ "$bashrun_window_id" == "$WINDOWID" ]]
}

function §window.update {

    bashrun_window_mapped=0
    if brwctl $bashrun_window_id mapped?; then 
	bashrun_window_mapped=1
    fi
    bashrun_window_width=$COLUMNS
    bashrun_window_height=$LINES
}

function §window.resize {
    case $1 in
	up)
            (( bashrun_window_height-=1 ))
	    ;;
	down)
            (( bashrun_window_height+=1 ))
	    ;;
	left)
            (( bashrun_window_width-=1 ))
	    ;;
	right)
            (( bashrun_window_width+=1 ))
	    ;;
    esac
    (( bashrun_window_height < 1 )) && bashrun_window_height=1
    (( bashrun_window_width < 1 )) && bashrun_window_width=1
    §window.size --hint $bashrun_window_width $bashrun_window_height
}

function §window.size {
    
    # get window size
    if (( $# == 0 )); then
	§window.update
	printf '%s' "$bashrun_window_width $bashrun_window_height"
	return 0;
    fi

    local hint="size"
    if [[ "$1" == "--hint" ]]; then
	hint="sizeh"
	shift
    fi

    if (( $# == 1 )); then
	if [[ "$1" =~ ([0-9]+)x([0-9]+) ]]; then
	    local w=${BASH_REMATCH[1]}
	    local h=${BASH_REMATCH[2]}
	    brwctl $bashrun_window_id $hint $w $h
	    bashrun_window_width=$w
	    bashrun_window_height=$h
	fi
    fi

    if (( $# == 2 )); then
	brwctl $bashrun_window_id $hint $1 $2
	bashrun_window_width=$1
	bashrun_window_height=$2
    fi
    COLUMNS=$bashrun_window_width
    LINES=$bashrun_window_height

    # apply completion threshold if necessary
    §window.terminal? && §completion.set $(§mode.get_completion)
    return 0
}
 
function §window.map {

    local states="$(§mode.get_netwmstate)"
    local state=""

    # apply default size if debugging
    if (( bashrun_debug )); then	 
	§window.size --hint "80x24"
    fi

    # remember the previously focused window
    bashrun_window_previous="$(brwctl focused)"

    # map the window
    brwctl $bashrun_window_id show

    # apply +mode --netwm-state
    if [[ -n "$states" ]]; then
	for state in $states; do
	    brwctl $bashrun_window_id state add "$state"
	done
    fi
    
    # call user supplied onmap handler
    $(§mode.get_onmap)
    
    bashrun_window_mapped=1
    return 0
}

function §window.unmap {

    (( $# == 0 )) && §window.update

    brwctl $bashrun_window_id unmap

    # call user supplied onunmap handler    
    $(§mode.get_onunmap)

    brwctl focus $bashrun_window_previous
    brwctl activate $bashrun_window_previous

    bashrun_window_mapped=0
    return 0
}

function §window.toggle { # [smart=0]
    
    local smart="${1:-0}"
    §window.update

    if (( bashrun_window_mapped )); then	
	if (( smart )); then
	    if brwctl $bashrun_window_id focus?; then
		§window.unmap
	    else
		§window.map
	    fi
	else
	    §window.unmap
	fi
    else
	§window.map
    fi
    return 0
}

function §window.toggle_smart {
    §window.toggle 1
}


