/*
 * PVVMUD a 3D MUD
 * Copyright (C) 1998-1999  Programvareverkstedet (pvv@pvv.org)
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <fstream>
#include <malloc.h>
#include "pvvmud.H"
#include "srvcliconsole.H"
#include "string.h"

const char * CSrvCliConsole::HISTORY_FILE = ".pvvmud_history";
 

CSrvCliConsole::CSrvCliConsole(CPvvmudClient * cli) : CConsole(cli) {
  m_numMsgs = 0;
  m_historyPos = 0;
  m_consSize = getCols()-1;
  loadHistory();
}
  
CSrvCliConsole::CSrvCliConsole(CPvvmudClient * cli, int cols, int rows) : CConsole(cli, cols, rows) {
  m_numMsgs = 0;
  m_historyPos = 0;
  m_consSize = getCols()-1;
  loadHistory();
}

void CSrvCliConsole::drawText() {
}

int CSrvCliConsole::getNumMsgs() {
  return m_numMsgs;
}

void CSrvCliConsole::setNumMsgs(int num) {
  m_numMsgs = num;
}

void CSrvCliConsole::addMsg(char * msg) {
  string s = msg;
  if (m_numMsgs >= m_consSize) {
    m_messages.erase(m_messages.begin());
  }
  m_messages.push_back(msg);
  m_numMsgs++;
  printMsgs();
}

void CSrvCliConsole::printMsgs() {
  const char * c;
  clear();
  for (int i=0;i<m_messages.size();i++) {
    c = m_messages[i].c_str();
    print((char *)c);
  }
}

void CSrvCliConsole::loadHistory() {
  ifstream * file = new ifstream(HISTORY_FILE);
  if (!file) {
    cout <<  "Error\n";
    exit(0);
  } // if
  filebuf * fb = file->rdbuf();
  char * buf = (char*)malloc(80);
  char c = 0;
  int i = 0;
  while(file->get(c)) {
    if (i >= 80 || c == '\n') {
      string s = string(buf);
      //cout << s;
      m_history.push_back(s);
      m_historyPos++;
      i = 0;
      memset(buf, 0, 80);
    } // if
    else {
      buf[i++] = c;
    } // else
  } // while
  file->close();
  free(buf);
  delete file;
}
    
void CSrvCliConsole::saveHistory() {
  ofstream * file = new ofstream(HISTORY_FILE);
  filebuf * fb = file->rdbuf();
  for (int i=0;i<m_history.size();i++) {
    fb->sputn(m_history[i].c_str(), m_history[i].size());
    fb->sputc('\n');
  } // for
  file->close();
  delete file;
}
void CSrvCliConsole::keyboardInput(char k) {
  switch (k) {
  case KEY_UP:
    m_input_x = 0;
    m_inputBuffer = "";
    clear();
    printMsgs();
    if (m_historyPos > -1) {
      m_historyPos--;
      if (m_historyPos > -1) {
	m_inputBuffer = m_history[m_historyPos];
	if (getEcho()) {
	  char c;
	  int i = 0;	
	  const char * cstr = m_inputBuffer.c_str();
	  while (c = cstr[i]) {
	    echoChar(c);
	    i++;
	  } // while
	} // if
      } // if
    } // if
    break;
  case KEY_DOWN:
    m_input_x = 0;
    m_inputBuffer = "";
    clear();
    printMsgs();
    if (m_historyPos == -1 || m_historyPos < m_history.size()) { //  Stygt hack
      m_historyPos++;
      if (m_historyPos < m_history.size()) {
	m_inputBuffer = m_history[m_historyPos];
	if (getEcho()) {
	  char c;
	  int i = 0;	
	  const char * cstr = m_inputBuffer.c_str();
	  while (c = cstr[i]) {
	    echoChar(c);
	    i++;
	  } // while
	} // if
      } // if
      else {
	m_inputBuffer = "";
	m_input_x = 0;
      } // else
    }
    break;
  case '\r':
    changeInputMode();
    m_historyPos = m_history.size();
    if (m_inputBuffer != "") {
      setDataReady();
      m_history.push_back(m_inputBuffer);
      m_historyPos++;
    }
    break;
  case '\b':
    if (m_inputBuffer != "") {
      m_inputBuffer.erase(m_inputBuffer.end()-1);
      if (getEcho()) {
	m_input_x--;
	m_console[m_input_y * getRows() + m_input_x] = 0;
      }
    }
    break;
  default:    
    if (getEcho()) {
      echoChar(k);
    }
    m_inputBuffer += k;
  }
}