#include <macros.h>
#include <stdproperties.h>
#include <cmdparse.h>
#include <composite.h>

inherit "/std/object.c";

string but_desc = "";
string opis_plynu_dop = "";
string opis_plynu_mia = "";
string opis_plynu_long = "";
string drink_msg = 0;
string valid_f = "";
int poj_calk = 1000;
int zawartosc = 0;
int alc;

//void set_but_long(string s);
// void set_opis_plynu(string a, string b, string c, string d);
mixed fill_living(object ob);
mixed fill_item(object ob);

void
create_butelka()
{
}

void
create_object()
{
    create_butelka();

    set_long("@@dlugi_opis");

    add_prop(OBJ_I_VOLUME, "@@vol_but");
    add_prop(OBJ_I_WEIGHT, "@@wei_but");
    add_prop(OBJ_I_VALUE, 0);
}

void
set_but_long(string str)
{
    but_desc = str;
}

void
set_opis_plynu(string mia, string dop, string long, string drink = 0)
{
    opis_plynu_mia = mia;
    opis_plynu_dop = dop;
    opis_plynu_long = long;
    drink_msg = drink;
}

void
set_pojemnosc(int i)
{
    poj_calk = i;
    zawartosc = i;
}

void
set_alc(int i)
{
    alc = i;
}

void
set_valid_f(string str)
{
    valid_f = str;
}

string
dlugi_opis()
{
    string str = but_desc + capitalize(short(PL_MIA)) + " ";
    
    switch (zawartosc * 100 / poj_calk)
    {
    case 0: str += "jest zupelnie pust" + koncowka("y", "a", "e"); break;
    case 1..20: str += "zawiera mala ilosc " + opis_plynu_dop; break;
    case 21..50: str += "zawiera troche " + opis_plynu_dop; break;
    case 51..70: str += "zawiera sporo " + opis_plynu_dop; break;
    case 71..90: str += "jest prawie peln" + koncowka("y", "a", "e") +
		     " " + opis_plynu_dop; break;
    default: str += "jest peln" + koncowka("y", "a", "e") + " " +
		 opis_plynu_dop; break;
    }

    str += ".\n";

    return str;
}

int
vol_but()
{
    return 6 * poj_calk / 5;
}

int
wei_but()
{
    return poj_calk / 4 + zawartosc;
}

int
nalej(string str)
{
    mixed *oblist1, *oblist2;
    string str2;
    int a, tmp;

    if (!str)
	str = "";

    notify_fail("Nalej czego z czego do czego? Nalej czego z czego komu?\n");
    
    if (!parse_command(str, all_inventory(this_player()), "%w 'z' %i:" + PL_DOP + " 'do' %i:" +
		       PL_DOP, str2, oblist1, oblist2))
    {
	if (!parse_command(str, all_inventory(environment(this_player())) + 
			       all_inventory(this_player()), "%w 'z' %i:" + 
			   PL_DOP + " %l:" + PL_CEL, str2, oblist1, oblist2))
	    return 0;
    }

    if (!wildmatch("*" + str2 + "*", opis_plynu_dop))
	return 0;

    oblist1 = NORMAL_ACCESS(oblist1, 0, 0);
    oblist2 = NORMAL_ACCESS(oblist2, 0, 0);

    if (!sizeof(oblist1) || oblist1[0] != this_object())
	return 0;

    if (sizeof(oblist1) > 1)
    {
	notify_fail("Nie mozesz nalewac wiecej z niz jednego naczynia naraz.\n");
	return 0;
    }

    if (!sizeof(oblist2))
	return 0;

    if (zawartosc == 0)
    {
	notify_fail(capitalize(short(PL_MIA)) + " jest juz calkowicie pust" + 
		    koncowka("y", "a", "e") + ".\n");
	return 0;
    }

    
    if (sizeof(oblist2) == 1)
    {
    	if (living(oblist2[0]))
	{
	    mixed res = fill_living(oblist2[0]);

	    if (intp(res) && res == 0)
	    {
		write(oblist2[0]->query_Imie(this_player(), PL_MIA) + " nie posiada nic " +
		      "w co mogl" + this_player()->koncowka("", "a", "o") + "bys " +
		      oblist2[0]->koncowka("mu", "jej", "mu") + " tego nalac.\n");


		return 1;
	    }
	    else
	    {
		string emp;
		if (!zawartosc)
		    emp = ", oprozniajac " + short(PL_BIE) + " zupelnie.\n";
		else
		    emp = ".\n";
		write("Nalewasz " + oblist2[0]->query_imie(this_player(), PL_CEL) + " " + 
		      opis_plynu_dop + " do " + res->short(this_player(), PL_DOP) + emp);

		oblist2[0]->catch_msg(QCIMIE(this_player(), PL_MIA) + " nalewa ci " +
				      opis_plynu_dop + " do " + QSHORT(res, PL_DOP) + emp);
		
		say (QCIMIE(this_player(), PL_MIA) + " nalewa " + QIMIE(oblist2[0], PL_CEL) +
		     " " + opis_plynu_dop + " do " + QSHORT(res, PL_DOP) + emp,
		     ({this_player(), oblist2[0]}));

		res->update_short();

		return 1;
	    } 
	}
	else // naczynie
	{
	    string emp;
	    int res = fill_item(oblist2[0]);

	    switch (res)
	    {
	    case 0:
		write(capitalize(oblist2[0]->short(this_player(), PL_MIA)) + " nie jest " +
		      "przeznaczon" + oblist2[0]->koncowka("y", "a", "e") + " do tego " +
		      "rodzaju trunkow.\n");
		return 1;
	    case 1:
		write(capitalize(oblist2[0]->short(this_player(), PL_MIA)) + " jest juz " +
		      "pel" + oblist2[0]->koncowka("en", "na", "ne") + ".\n");
		return 1;
	    case 2:
		if (!zawartosc)
		    emp = ", oprozniajac " + short(PL_BIE) + " zupelnie.\n";
		else
		    emp = ".\n";
		write("Nalewasz " + opis_plynu_dop + " do " + oblist2[0]->short(this_player(),
		      PL_DOP, 1) + emp);
		say(QCIMIE(this_player(), PL_MIA) + " nalewa " + opis_plynu_dop + " do " +
		    QSHORT(oblist2[0], PL_DOP) + emp);

		oblist2[0]->update_short();

		return 1;
	    }
	}
    }
    else
    {
	string emp;
	if (living(oblist2[0]))
	{
	    object *happy = ({ });
	    object otmp, *items = ({ });
	    tmp = sizeof(oblist2);
	    for (a = 0; a < tmp; a++)
	    {
		if (objectp(otmp = fill_living(oblist2[a])))
		{
		    happy += ({ oblist2[a] });
		    items += ({ otmp });
		}
	    }
	    
	    if (!sizeof(happy))
	    {
		write("Nie nalales nic nikomu.\n");
		return 1;
	    }

	    if (!zawartosc)
		emp = ", oprozniajac " + short(PL_BIE) + " zupelnie.\n";
	    else
		emp = ".\n";

	    write("Nalewasz " + opis_plynu_dop + " " + COMPOSITE_LIVE(happy, PL_CEL) + emp);
	    
	    say(QCIMIE(this_player(), PL_MIA) + " nalewa " + opis_plynu_dop + " " +
		QCOMPLIVE(PL_CEL) + emp, ({ this_player() }) + happy);

	    happy->catch_msg(QCIMIE(this_player(), PL_MIA) + " nalewa ci " + opis_plynu_dop +
			     emp);
	    items->update_short();

	    return 1;
	}
	else // naczynia
	{
	    object *items = ({ });
	    tmp = sizeof(oblist2);
	    for (a = 0; a < tmp; a++)
	    {
		if (fill_item(oblist2[a]) == 2)
		    items += ({ oblist2[a] });		
	    }
	    
	    if (!sizeof(items))
	    {
		write("Nie nalales nic do niczego.\n");
		return 1;
	    }

	    if (!zawartosc)
		emp = ", oprozniajac " + short(PL_BIE) + " zupelnie.\n";
	    else
		emp = ".\n";

	    write("Nalewasz " + opis_plynu_dop + " do " + COMPOSITE_DEAD(items, PL_DOP) + emp);
	    
	    say(QCIMIE(this_player(), PL_MIA) + " nalewa " + opis_plynu_dop + " do " +
		QCOMPDEAD(PL_DOP) + emp);
	    items->update_short();

	    return 1;
	}
		  
    }
    return 1;
}

mixed
fill_item(object ob)
{
    if (!call_other(ob, valid_f))
	return 0;

    if (ob->query_full())
	return 1;

    if (zawartosc == 0)
	return -1;

    ob->fill_it(alc, opis_plynu_dop, opis_plynu_long, drink_msg);

    zawartosc -= ob->query_liquid_vol();
    if (zawartosc < 0) zawartosc = 0;
    
    return 2;
}

mixed
fill_living(object ob)
{
    object *oblist = all_inventory(ob);
    int i, tmp;

    tmp = sizeof(oblist);
    for (i = 0; i < tmp; i++)
    {
	if (fill_item(oblist[i]) == 2)
	    return oblist[i];
    }

    return 0;
}

void
init()
{
    add_action(nalej, "nalej");
    ::init();
}

string
stat_object()
{
    string str = ::stat_object();

    str += "Zaw. alkoholu: " + alc + "\n";
    str += "Poj. calkowita: " + poj_calk + "\t\tAkt. zawartosc: " + zawartosc + "\n";
    str += "Funkcja wywolywana w naczyniu: " + valid_f + "\n";
    str += "Opis plynu mian.: " + opis_plynu_mia + "\n";
    str += "Opis plynu dop.: " + opis_plynu_dop + "\n";
    str += "Dlugi opis plynu: " + opis_plynu_long;
    str += "Tekst wypisywany pijacemu: " + drink_msg;

    return str;
}
