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

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

local ObjectTable = GetObjectTable("timetosettable");

dinnertable1 = simple_object:new{

	character_cleaning_animation = "doing_bow_down",
	parent_state_action = "holding_plates",
	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,
	
	check_teamwork = false;
	finish_teamwork = true;
	teamworktask_name = "setthetable";
	
	complex_object = true;
	dad_next_task_x = indicator_pos_x-20,
	dad_next_task_y = indicator_pos_y-20,
	mom_next_task_x = indicator_pos_x-20,
	mom_next_task_y = indicator_pos_y-20,
	
	first_kid_sat = true;
	
	stress_inc_enabled = false;

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

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

function dinnertable1:clean_schoolaged_action(character_name)
	self:sit_character(character_name);	
end

function dinnertable1:messing_schoolaged_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:dirty_schoolaged_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:flaging_schoolaged_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:cleaning_schoolaged_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:with_plates_schoolaged_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:clean_youngchild_action(character_name)
	self:sit_character(character_name);
	--PlayObjectAnimation("dispensa", "dirty");
	--ChangeObjectState("dispensa", "dirty");	
end

function dinnertable1:messing_youngchild_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:dirty_youngchild_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:flaging_youngchild_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:cleaning_youngchild_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:with_plates_youngchild_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:clean_toddler_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:messing_toddler_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:dirty_toddler_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:flaging_toddler_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:cleaning_toddler_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:with_plates_toddler_action(character_name)
	self:sit_character(character_name);
end

function dinnertable1:restarttable_update()
	self.first_kid_sat = true;
	ChangeObjectState(self.object_name, "clean");
end

function dinnertable1:clean_init()
	self.first_kid_sat = true;
	PlayObjectAnimation(self.object_name, self.object_clean_animation);
	PlayGlowAnimation(self.object_name, "clean");
	
	self.object_is_dirty = false;
	--self.parent_chosen = "";
	self.click_enable = self.set_click_enable;
	self.parent_is_coming = false;
	
	if self.click_enable then
		IncludeObjectMouseHandler(self.object_name);
	end
	
	self.previous_state = GetObjectState(self.object_name);
	
	if self.complex_object then
		HideNextTaskIcon(self.object_name);
		HideDadNextTaskIcon(self.object_name);
		HideMomNextTaskIcon(self.object_name);
	end
end

function dinnertable1:cancel()
	
	self.parent_is_coming = false;
	
	if GetCharacterState(self.parent_chosen) ~= "cleaning" and
		GetCharacterState(self.parent_chosen) ~= "sit" and
		GetCharacterState(self.parent_chosen) ~= "sit_outside" and 
		GetCharacterState(self.parent_chosen) ~= "sit_to_play"	then
		i, j = string.find(GetCharacterState(self.parent_chosen), "holding");
		if i ~= 1 then
			PlayCharacterAnimation(self.parent_chosen, "idle");
			SetCharacterState(self.parent_chosen, "normal");
		else			
			PlayCharacterAnimation(self.parent_chosen, "standing_with_stuff");
			ShowCharacterStuff(self.parent_chosen, GetCharacterStuff(self.parent_chosen), "0", "0", "0");
		end		
	end
	
	
	
	if self.parent_chosen == "dad" then
		HideDadIcon(self.object_name);
	else
		HideMomIcon(self.object_name);
	end
	
	RemoveObjectFromClickedList(self.object_name);
	if self.previous_state == "clean" then
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		PlayGlowAnimation(self.object_name, "clean");
		
		self.object_is_dirty = false;
		--self.parent_chosen = "";
		self.click_enable = self.set_click_enable;
		self.parent_is_coming = false;
		
		if self.click_enable then
			IncludeObjectMouseHandler(self.object_name);
		end
		
		self.previous_state = GetObjectState(self.object_name);
		
		if self.complex_object then
			HideNextTaskIcon(self.object_name);
			HideDadNextTaskIcon(self.object_name);
			HideMomNextTaskIcon(self.object_name);
		end
	elseif self.previous_state ~= "messing" then
		ChangeObjectState(self.object_name, self.previous_state);
	end
end

function dinnertable1:sit_character(character_name)
	if self.first_kid_sat then
		--PlayObjectAnimation("dispensa", "dirty");
		ChangeObjectState("dispensa", "mealtime");
		ShowBreakSign("meal");
		self.first_kid_sat = false;
	end
	PlayCharacterAnimation(character_name, "sit");
	SetCharacterState(character_name, "sit");
	HideCharacter(character_name);
	ChangeObjectState(character_name.."eating", "idle");
	PutCharacterOnObject(character_name, "mesajantar3");
end

--[[
function dinnertable1:dirty_mouse_on()
	if not self.mouse_on then
		self.mouse_on = true;
		if GetCharacterState("dad") == self.parent_state_action then
			ShowDadIcon(self.object_name);
		end
		if GetCharacterState("mom") == self.parent_state_action then
			ShowMomIcon(self.object_name);
		end
		PlayObjectAnimation(self.object_name, self.object_dirty_mouse_on_animation);
	end
end
--]]

function dinnertable1: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
		--[[
		if self.parent_chosen == "dad" then
			PlaySound("working_father", true);
		else
			PlaySound("working_mother", true);
		end
		--]]
		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();
		PlaySound("set_table", true);
	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 dinnertable1:cleaning_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		if self.finish_teamwork then
			FinishTeamWorkTask(self.parent_chosen, self.teamworktask_name, 1);
		end
		
		if self.check_teamwork then
			ShowTeamWorkIcon(self.object_name, 2);
			--ShowTeamWorkIcon(self.object_name, "mom", false);		
		end
		StopSound("set_table");
		if self.parent_chosen == "dad" then
			--StopSound("working_father");
			PlaySound("task_complete_father", false);
			HideDadNextTaskIcon(self.object_name);
		else
			--StopSound("working_mother", true);
			PlaySound("task_complete_mother", false);
			HideMomNextTaskIcon(self.object_name);
		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;
		FinishCharacterTask(self.parent_chosen);
		
		PlayGlowAnimation(self.object_name, "clean");
		
		--PlayObjectAnimation(self.object_name, "with_plates");
		--ChangeObjectState(self.object_name, "with_plates");
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		ChangeObjectState(self.object_name, "with_plates");
		
		self.previous_state = "with_plates";
		
		self.click_enable = false;
		
		PlayObjectAnimation("geladeira", "dirty");
		ChangeObjectState("geladeira", "mealtime");
		ShowNextTaskIcon("geladeira");
		
		ChangeObjectState("mesajantar2", "with_plates");
		
		self.cleaning_state_time = self.previous_cleaning_state_time;
	end
end