require( "scripts/common/simple_object.lua" );

local	indicator_pos_x = -5;
local	indicator_pos_y = -20;

local ObjectTable = GetObjectTable("timetosettablewithfood");

dinnertable2 = simple_object:new{

	character_cleaning_animation = "doing_bow_down",
	parent_state_action = "holding_cooked_food",
	anchor_x=83,
	anchor_y=135,
	indicator_ui_x=indicator_pos_x,
	indicator_ui_y=indicator_pos_y,
	dad_ui_x=indicator_pos_x-35,
	dad_ui_y=indicator_pos_y,
	mom_ui_x=indicator_pos_x+35,
	mom_ui_y=indicator_pos_y,
	
	sleeping_symbol_x = "0";
	sleeping_symbol_y = "0";
	sleeping_symbol_layer = "2";
	
	cleaning_state_time = 2000;
	
	indicator_ui_x=indicator_pos_x,
	indicator_ui_y=indicator_pos_y,
	dad_ui_x=indicator_pos_x-35,
	dad_ui_y=indicator_pos_y,
	mom_ui_x=indicator_pos_x+35,
	mom_ui_y=indicator_pos_y,
	
	kids_on_table = false;
	is_with_food = false;
	
	eating_state_time = 3000,
	
	mom_current_state = "";
	mom_current_stuff = "";
	dad_current_state = "";
	dad_current_stuff = "";
	
	complex_object = true;
	dad_next_task_x = indicator_pos_x-35,
	dad_next_task_y = indicator_pos_y,
	mom_next_task_x = indicator_pos_x+35,
	mom_next_task_y = indicator_pos_y,
	
	child_members = {};
	dad_coming = false;
	mom_coming = false;
	
	stress_inc_enabled = false;

	cleaning_state_time = ObjectTable["cleaning_time"];
	
	is_a_mess = false,
}

function dinnertable2:new(o)
	o = o or {}   -- create object if user does not provide one
	setmetatable(o, self)
	self.__index = self
	return o
end

function dinnertable2:sit_character(character_name)
	PlayCharacterAnimation(character_name, "sit");
	SetCharacterState(character_name, "sit");
	HideCharacter(character_name);
	ChangeObjectState(character_name.."eating", "idle");
	PutCharacterOnObject(character_name, "mesajantar3");
end

function dinnertable2:parent_command()
	DebugOut("simple_object:parent_command:"..self.object_name);
	RemoveObjectMouseHandler(self.object_name);
	if self.parent_chosen == "dad" then
		AddCharacterTask(self.parent_chosen, self.object_name, "gotoanddo", self.dad_chair);
	else
		AddCharacterTask(self.parent_chosen, self.object_name, "gotoanddo", self.mom_chair);
	end
end

function dinnertable2:parent_action()
	self.parent_is_coming = false;
	if self.parent_chosen == "dad" then
		HideDadIcon(self.object_name);
	else
		HideMomIcon(self.object_name);
	end
	RemoveObjectFromClickedList(self.object_name);
	if GetCharacterState(self.parent_chosen) == self.parent_state_action and self.object_is_dirty then
		HideCharacterStuff(self.parent_chosen);
		PlayCharacterAnimation(self.parent_chosen, self.character_cleaning_animation);
		FlipAnimation(self.parent_chosen, self.flipped_animation);
		PlayObjectAnimation(self.object_name, self.object_cleaning_animation);
		ChangeObjectState(self.object_name, self.cleaning_state_name);
		self.new_state_start_time = GetCurrentTime();
		SetCharacterState(self.parent_chosen, self.cleaning_state_name);
		
		self:get_cleaning_state_time();
	else
		ShowMiss(self.object_name);
		if self.no_stress_power then
			HideNoStressIcon(self.object_name);
		end
		self.no_stress_power = false;
		IncludeObjectMouseHandler(self.object_name);
		FinishCharacterTask(self.parent_chosen);
		self.parent_chosen = "";
		self.click_enable = self.set_click_enable;
		if self.object_is_dirty then
			--ChangeObjectState(self.object_name, self.dirty_state_name);			
			IncludeObjectMouseHandler(self.object_name);			
			self.click_enable = true;
			PlayObjectAnimation(self.object_name, self.object_dirty_animation);	
			--PlayGlowAnimation(self.object_name, "dirty");
		end
	end
end

function dinnertable2:with_plates_init()
	self.previous_state = "with_plates";
end

function dinnertable2:with_plates_update()
	PlayObjectAnimation("mesajantar2", "dirty");
end

function dinnertable2:cleaning_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		if self.parent_chosen == "dad" then
			StopSound("working_father");
			PlaySound("task_complete_father", false);
			HideDadNextTaskIcon(self.object_name);
			ShowMomNextTaskIcon("mesajantar3");
		else
			StopSound("working_mother");
			PlaySound("task_complete_mother", false);
			HideMomNextTaskIcon(self.object_name);
			ShowDadNextTaskIcon("mesajantar3");
		end
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);	
		SetCharacterState(self.parent_chosen, "normal");
		if self.stress_inc_enabled and not self.no_stress_power then
			--AddCharacterStress(self.parent_chosen, self.stress_inc);
		end
		if self.no_stress_power then
			HideNoStressIcon(self.object_name);
		end
		self.no_stress_power = false;
		
		self:sit_character(self.parent_chosen);
		--FinishCharacterTask(self.parent_chosen);
		
		PlayGlowAnimation(self.object_name, "clean");
		
		--PlayObjectAnimation(self.object_name, "with_dirty_plates");
		--ChangeObjectState(self.object_name, "with_dirty_plates");
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		ChangeObjectState(self.object_name, "with_dirty_plates");
		self.previous_state = "with_dirty_plates";
		self.click_enable = false;		
		
		PlayObjectAnimation("mesajantar3", "dirty");
		ChangeObjectState("mesajantar3", "dirty");
		
		self.cleaning_state_time = self.previous_cleaning_state_time;
	end
end