/*-
 * Copyright (c) 2000
 * Tatsuya Kudoh(CDR/TK),ROYALPANDA.    All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include<stdio.h>
#include<stdlib.h>
#include<sys/bios.h>
#include"jump.h"

static int Name[16];
static int Cursor;

#define CURSOR	(14<<9 | 0x1e)
#define BASE	(12<<9 | '-')

static const char Chr_src[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x3\xd+-?,";
#define NUM_CHRS	43
static unsigned Chr[NUM_CHRS];


static int decode( int ch )
{
	int i;

	for( i = 0 ; i < NUM_CHRS ; i++ )
		if( Chr_src[i] == ch )
			return i;
	return 0;
}


void ENTRY_init( void )
{
	int i;
	char buf[16];

	for( i = 0 ; i < NUM_CHRS ; i++ )
		Chr[i] = 14<<9 | (unsigned)Chr_src[i];

	sys_get_ownerinfo(16,(char far*)buf);
	Cursor = 0;
	while( Cursor < 16 && buf[Cursor] ){
		Name[Cursor] = decode(buf[Cursor]);
		Cursor++;
	}

	i = Cursor;
	while( i < 16 ){
		Name[i] = 0;
		i++;
	}

	if( Cursor )
		Cursor--;
}


static void put_chr( int pos )
{
	screen_fill_char(1,pos+12,6,1,1,Chr[Name[pos]]);
}


static void put_cursor( void )
{
	screen_fill_char(1,Cursor+12,7,1,1,CURSOR);
}


static void erase_cursor( void )
{
	screen_fill_char(1,Cursor+12,7,1,1,BASE);
}


static void select_chr( int dir )
{
	Name[Cursor] = (Name[Cursor] + dir + NUM_CHRS) % NUM_CHRS;
	put_chr(Cursor);
}


static void cursor_next( void )
{
	if( Cursor < 15 ){
		erase_cursor();
		Cursor++;
		put_cursor();
	}
}

static void cursor_back( void )
{
	Name[Cursor] = 0;
	put_chr(Cursor);

	if( Cursor > 0 ){
		erase_cursor();
		Cursor--;
		put_cursor();
	}
}


void ENTRY_edit( int score )
{
	int i;
	int k;
	char buf[16];

	DISP_clear();

	sprintf(buf,"Floor %5d",score);
	DISP_put_str(12,1,(char far*)buf);
	DISP_put_str(4,4,"Enter your name:");

	for( i = 0 ; i < 16 ; i++ )
		put_chr(i);

	screen_fill_char(1,12,7,16,1,BASE);

	put_cursor();

	DISP_put_str(15,10,"X1,X3 : Select");
	DISP_put_str(18,11,   "X2 : Next");
	DISP_put_str(18,12,   "X4 : Delete");
	DISP_put_str(15,14,"START : Accept");

	while( !((k = KEY_read(2)) & K_START) ){
		switch( k ){
		case K_UP:
			select_chr(1);
			break;

		case K_DOWN:
			select_chr(-1);
			break;

		case K_RIGHT:
			cursor_next();
			break;

		case K_LEFT:
			cursor_back();
			break;
		}
		GAME_scroll_back();
	}
}


/* ENTRY_get_name : no NULL-termination, return 16 chrs */

void ENTRY_get_name( char far *buf )
{
	int i;

	for( i = 0 ; i < 16 ; i++ )
		buf[i] = Chr_src[Name[i]];
}
