#define I_MIN_MORALE	10

int nPaymentQ;
int nMoraleDecreaseQ;

void InitInterface(string iniName)
{
    GameInterface.title = "titleSalary";

    SendMessage(&GameInterface,"ls",MSG_INTERFACE_INIT,iniName);

	CalculateInfoData();

	int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	SetFormatedText("INFO_TEXT",LanguageConvertString(tmpLangFileID,"Salary info"));
	LanguageCloseFile(tmpLangFileID);
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_MSG_TO_NODE,"INFO_TEXT",5);

	SetEventHandler("InterfaceBreak","ProcessBreakExit",0)
	SetEventHandler("exitCancel","ProcessCancelExit",0);
	SetEventHandler("ievnt_command","ProcCommand",0);
	SetEventHandler("evntDoPostExit","DoPostExit",0);
}

void ProcessBreakExit()
{
	SkipSailorPayment();
	IDoExit( RC_INTERFACE_SALARY_EXIT );
}

void ProcessCancelExit()
{
	SkipSailorPayment();
	IDoExit( RC_INTERFACE_SALARY_EXIT );
}

void IDoExit(int exitCode)
{
	DelEventHandler("InterfaceBreak","ProcessBreakExit");
	DelEventHandler("exitCancel","ProcessCancelExit");
	DelEventHandler("ievnt_command","ProcCommand");
	DelEventHandler("evntDoPostExit","DoPostExit");

	interfaceResultCommand = exitCode;
	EndCancelInterface(true);
}

void ProcCommand()
{
	string comName = GetEventData();
	string nodName = GetEventData();

	switch(nodName)
	{
	case "B_OK":
		if(comName=="activate" || comName=="click")
		{
			ExecuteSailorPayment();
			PostEvent("evntDoPostExit",1,"l",RC_INTERFACE_SALARY_EXIT);
		}
	break;

	case "B_CANCEL":
		if(comName=="activate" || comName=="click")
		{
			SkipSailorPayment();
			PostEvent("evntDoPostExit",1,"l",RC_INTERFACE_SALARY_EXIT);
		}
		if(comName=="upstep")
		{
			if(GetSelectable("B_OK"))	{SetCurrentNode("B_OK");}
		}
	break;
	}
}

void ExecuteSailorPayment()
{
	ref PChar = GetMainCharacter();
	AddMoneyToCharacter(GetMainCharacter(),-nPaymentQ);
//JA 26Nov06 -> pay officers on other ships, and use daily pay system
	PChar.quest.CrewPayOwed = 0;
	// NK -->
	int i, j, cn, on;
	for(i = 0; i < 4; i++)
	{
		cn = GetCompanionIndex(PChar, i);
		if(cn == -1) continue;
		if (IsTrader(GetCharacter(cn))) continue; // KK
		if(cn!=sti(PChar.index)) PayOfficerOwedMoney(Characters[cn]); 
		for(j = 1; j < 4; j++)
		{
			on = GetOfficersIndex(Characters[cn], j);
			if(on == -1) continue;
			PayOfficerOwedMoney(Characters[on]); 
		}
	}
	for(i = 0; i < GetPassengersQuantity(pchar); i++)
	{
		cn = GetPassenger(pchar, i);
		if(cn == -1) continue;
		ref curChar = GetCharacter(cn);
		if(CheckAttribute(curChar,"prisoned") || IsOfficer(curChar) || IsTrader(curChar)) continue;  //don't pay if prisoner, trader or already payed above. // KK
		PayOfficerOwedMoney(Characters[cn]); 
	}

// <-- JA
	int pSalary = GetPaymentSum(PChar);//MAXIMUS
	if(GetCharacterMoney(PChar) > pSalary)
	{
		AddMoneyToCharacter(GetMainCharacter(),-pSalary);
		AddWealthToCharacter(GetMainCharacter(), pSalary);
	}
	else
	{
		AddMoneyToCharacter(GetMainCharacter(), GetCharacterMoney(PChar));
		AddWealthToCharacter(GetMainCharacter(), GetCharacterMoney(PChar));
	}
	DeleteAttribute(GetMainCharacter(),"foodoff"); // reset food off toggle
	// NK <--
	//DeleteAttribute(GetMainCharacter(),"CrewPayment");  JA 27Nov06 CrewPayment not used to track crew money owed anymore.
}
//MAXIMUS -->
int GetPaymentSum(ref PChar)
{
	int pSalary = makeint((0.75 + makefloat(GetFoodEver()) * 0.25) * makefloat((10+sti(PChar.rank)*2) * 100 * COMP_PRICE_SCALAR* SALARY_MULTIPLIER) * (0.5 * makefloat(GetDifficulty())));
	return pSalary;
}
//MAXIMUS <--
void PayOfficerOwedMoney(ref chref)
{
	if(!CheckAttribute(chref,"quest.OfficerPrice") || !GetRemovable(&chref)) return;//not a payable officer
	if(CheckAttribute(chref,"quest.OfficerPayOwed"))
	{
		AddWealthToCharacter(&chref, sti(chref.quest.OfficerPayOwed));
		chref.quest.OfficerPayOwed = 0;
	}
}


void SkipSailorPayment()
{
	ref mchref = GetMainCharacter();
	int morale = 45;
//MAXIMUS: for new Mutiny -->
	int debt = GetPaymentSum(mchref);
	mchref.crewdebt = sti(mchref.crewdebt) + debt;
//MAXIMUS: for new Mutiny <--
	if( CheckAttribute(mchref,"Ship.Crew.Morale") ) morale = sti(mchref.Ship.Crew.Morale);
	morale -= nMoraleDecreaseQ;
	if(morale<0)	morale = 0;

	//mchref.CrewPayment = nPaymentQ; //JA 28Nov06 Not required as money owed to crew now stored elsewhere

	int cn;
	for(int i=0; i<4; i++)
	{
		cn = GetCompanionIndex(mchref,i);
		if(cn>=0)
		{
			Characters[cn].Ship.Crew.Morale = morale;
		}
	}

	if(morale<MORALE_NORMAL)
	{
		if(morale<=I_MIN_MORALE)
		{
			KAM_Mutiny();//MAXIMUS: new "Mutiny"
			//PostEvent("ievent_GameOver",1,"s","mutiny");
		}
	}
}

void DoPostExit()
{
	int exitCode = GetEventData();
	IDoExit(exitCode);
}

void CalculateInfoData()
{
	int i,cn,crewQ;
	ref mchref,chref;

	crewQ = 0;
	mchref = GetMainCharacter();

	// NK -->
	nPaymentQ = CalcSquadronPayment(mchref);
	int nLeadership = GetSummonSkillFromName(mchref,SKILL_LEADERSHIP);

	/* JA 27Nov 06 Money owed to crew stored elsewhere
	if( CheckAttribute(mchref,"CrewPayment") ) {
		nPaymentQ += makeint( stf(mchref.CrewPayment) ); // NK <--
	}
	*/

	nMoraleDecreaseQ = 30-nLeaderShip;
	if( CheckCharacterPerk(mchref,"IronWill") ) nMoraleDecreaseQ /= 2;

	CreateString(true,"payment",""+nPaymentQ,FONT_NORMAL,COLOR_NORMAL,320,258,SCRIPT_ALIGN_CENTER,1.0);

	if( sti(mchref.Money) < nPaymentQ )
	{
		SetSelectable("B_OK",false);
		SetCurrentNode("B_CANCEL");
	}
}
