# screenoutput: ZOMG screen output functions
#   Copyright (C) 2008-2013  Clint Adams

# 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 <http://www.gnu.org/licenses/>.

zomgcurses_drawwindows() {
  # This should be more dynamic; tw probably only needs to be this long
  # if the window is narrow.
  zcurses addwin tw 11 $(( COLUMNS )) 0 0
  zcurses border tw
  zcurses move tw 1 1
  zcurses attr tw green/black
  zcurses refresh tw
  zcurses addwin mw 3 $(( COLUMNS )) 11 0
  zcurses border mw
  zcurses move mw 1 1
  zcurses attr mw cyan/black
  zcurses refresh mw
  zcurses addwin bw $(( LINES - 14 )) $(( COLUMNS )) 14 0
  zcurses border bw
  zcurses move bw 1 1
  zcurses refresh bw
}

zomgcurses_init() {
  if (( COLUMNS < 40 )) || (( LINES < 20 )); then
    print "Your screen is too small for the curses interface."
    return 1
  fi
  zcurses init || return 1
  wehavecurses=1

  zomgcurses_drawwindows
  trap 'zcurses refresh' WINCH
}

writetowindow() {
# check for right number of args
#
local win="$1"
local str="$2"
local frag mark

zcurses position $win winloc
# y x cornery cornerx sizey sizex

if (( $winloc[2] + $#str < $winloc[6] )); then
  if (( $winloc[1] + 3 > $winloc[5] )); then
    zcurses scroll $win -1
    zcurses scroll $win 3
    zcurses border $win
    zcurses move $win $(( $winloc[1] - 2 )) 1
  fi
  zcurses string $win "$str"
  zcurses position $win winloc
  zcurses move $win $(( $winloc[1] + 1 )) 1
  zcurses refresh $win
else
  mark=${${str[1,$winloc[6]]}[(I) ]}
  if (( $mark < 1 )) || (( $mark > $winloc[6] - 2 )); then
    (( mark = $winloc[6] - 2 ))
  fi
  frag=$str[1,$mark]
  str=$str[$mark+1,-1]
  writetowindow $win "${frag}"
  writetowindow $win "${str}"
fi
}

status_msg() {
  if (( wehavecurses == 1 )); then
    writetowindow bw "$1"
    zcurses refresh bw
  else
    print "$1"
  fi
}

play_msg() {
  if (( wehavecurses == 1 )); then
    writetowindow tw "$1"
    zcurses refresh tw
  else
    print "$1"
  fi
}

current_msg() {
  if (( wehavecurses == 1 )); then
    writetowindow mw "$1"
    zcurses refresh mw
  else
    print "Station: $1"
  fi
}

time_status_msg() {
  status_msg "$(strftime "[%H:%M]" $EPOCHSECONDS) $1"
}

log_msg() {
  print -r  -- "$(strftime "[%H:%M:%S]" $EPOCHSECONDS) $1" >>$zomgdatadir/log
}

scrollclear() {
  case "$1" in
    (tw)
      zcurses scroll tw 11 && zcurses move tw 1 1 && zcurses border tw
      ;;
    (mw)
      zcurses scroll mw 3 && zcurses move mw 1 1 && zcurses border mw
      ;;
    (*)
      zcurses scroll $i 20 && zcurses move $i 1 1 && zcurses border $i
      ;;
  esac
}

truncate_log_new() {
  zmodload -F zsh/stat b:zstat
  if [[ $(zstat +size $zomgdatadir/log) -gt 100000 ]]; then
    tail -n 1000 $zomgdatadir/log >$zomgdatadir/log.tmp
    mv $zomgdatadir/log.tmp $zomgdatadir/log
  fi
}

truncate_log_ancient() {
    tail -n 1000 $zomgdatadir/log >$zomgdatadir/log.tmp
    mv $zomgdatadir/log.tmp $zomgdatadir/log
}

autoload -Uz is-at-least
if is-at-least 4.3.9; then
  truncate_log() { truncate_log_new "$@"; };
else
  truncate_log() { truncate_log_ancient "$@"; };
fi
