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

local	indicator_pos_x = -19;
local	indicator_pos_y = -80;

local	parent_getting_down = true;

local ObjectTable = GetObjectTable("naughtychair");

naughtychair = simple_object:new{
	anchor_x=0,
	anchor_y=20,
	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,
	cleaning_state_time = 1500,
	texture="anim/objects/naughtychair/naughtychair",
	animation_script="anim/objects/naughtychair/naughtychair_animation_script.lua",
	glow_texture="anim/objects/naughtychair/naughtychair_glow",
	glow_animation_script="anim/objects/default_glow_animation_script.lua",
	
	node_to_do_action = "",
	flip_animation = false,
	set_click_enable = false;
		
	mask = true;
	maskX = 0;
	maskY = -30;
	maskWidth = 92;
	maskHeight = 90;
	maskAlpha = 0.01;
	
	show_cleaning_particle = false;
	
	messing_state_time = 0;
	cleaning_state_time = 20000;
	
	complex_object = true;
	next_task_x = indicator_pos_x-39,
	next_task_y = indicator_pos_y,
	
	cleaning_timer_x = 0;
	cleaning_timer_y = -90;
	
	stress_inc_enabled = false;
	
	check_teamwork = false;
	finish_teamwork = true;
	teamworktask_name = "sendtonaughtyspot";

	messing_state_time = 0,
	cleaning_state_time = ObjectTable["cleaning_time"],
	stress_inc = ObjectTable["stress_inc"],
}

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

function naughtychair:clean_mess_action()
	HideCharacter(self.current_action_character);
	ChangeObjectState(self.current_action_character.."sitnaughty", "idle");
	PlayObjectAnimation(self.object_name, self.object_messing_animation);
	ChangeObjectState(self.object_name, self.messing_state_name);
	self.new_state_start_time = GetCurrentTime();
	
	self.previous_state = "messing";
end

function naughtychair:messing_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.messing_state_time then
		if self.sound_of_messing ~= "" then
			StopSound(self.sound_of_messing);
		end
		ChangeObjectState(self.object_name, self.dirty_state_name);
	end
end	

function naughtychair:parent_command()
	RemoveObjectMouseHandler(self.object_name);
	AddCharacterTask(self.parent_chosen, self.object_name, "gotoanddo", self.node_to_do_action);
end

function naughtychair:parent_action()	
	self.parent_is_coming = false;
	DebugOut("simple_object:parent_action:"..self.object_name);
	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.sound_of_work_enable then
			if self.parent_chosen == "dad" then
				PlaySound("working_father", true);
			else
				PlaySound("working_mother", true);
			end
		end
		
		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;
		
		ChangeObjectState(self.current_action_character.."sitnaughty", "playing");
		
		FinishCharacterTask(self.parent_chosen);
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);
		FlipAnimation(self.parent_chosen, self.flipped_animation);
		SetCharacterState(self.parent_chosen, "normal");
		
		PlayObjectAnimation(self.object_name, self.object_cleaning_animation);
				
		ChangeObjectState(self.object_name, self.cleaning_state_name);
		
		self.new_state_start_time = GetCurrentTime();
		self:get_cleaning_state_time();
		
		if self.sound_of_cleaning ~= "" then
			PlaySound(self.sound_of_cleaning, true);
		end		
		
		HideNextTaskIcon(self.object_name);
	else
		ShowMiss(self.object_name);
		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 naughtychair:cleaning_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		
		ShowCharacter(self.current_action_character);
		RemoveCharacterOnObject(self.current_action_character);
		FinishCharacterTask(self.current_action_character);		
		
		ChangeObjectState(self.current_action_character.."sitnaughty", "clean");
		ShowCharacter(self.current_action_character);
		
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		ChangeObjectState(self.object_name, self.clean_state_name);
		
		if self.finish_teamwork then
			FinishTeamWorkTask(self.parent_chosen, self.teamworktask_name, 1);
		end
		
		if self.check_teamwork then
			ShowTeamWorkIcon(self.object_name, 2);
		end
		
		self.cleaning_state_time = self.previous_cleaning_state_time;		
	end
end

function naughtychair:waitingchild_youngchild_action(character_name)
	self.current_action_character = character_name;	
	self:clean_mess_action();
end

function naughtychair:waitingchild_toddler_action(character_name)
	self.current_action_character = character_name;
	self:clean_mess_action();
end

function naughtychair:waitingchild_schoolaged_action(character_name)
	self.current_action_character = character_name;
	self:clean_mess_action();
end

function naughtychair:waitingchild_pet_action(character_name)
	self.current_action_character = character_name;
	self:clean_mess_action();
end
