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

local	indicator_pos_x = 0;
local	indicator_pos_y = -46;

local	parent_getting_down = true;

local ObjectTable = GetObjectTable("naughtyspot");

naughtyspot = simple_object:new{
	--current_action_character="toddler",
	character_messing_animation="naughty",
	character_dirty_animation="sit_crying",
	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/fall_floor/fall_floor",
	animation_script="anim/objects/default_animation_script.lua",
	glow_texture="anim/objects/fall_floor/fall_floor_glow",
	glow_animation_script="anim/objects/default_glow_animation_script.lua",
	
	node_to_do_action = "",
	flip_animation = false,
	set_click_enable = false;
	
	star_x = "10",
	star_y = "-60",
	star_layer = "1",
	
	mask = true;
	maskX = 0;
	maskY = -30;
	maskWidth = 92;
	maskHeight = 90;
	maskAlpha = 0.01;
	
	show_cleaning_particle = false;
	
	messing_state_time = 0;
	
	cleaning_timer_x = 30;
	cleaning_timer_y = -150;
	
	complex_object = true;
	
	stress_inc_enabled = false;
	
	check_teamwork = true;
	finish_teamwork = false;
	teamworktask_name = "sendtonaughtyspot";

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

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

function naughtyspot:clean_mess_action()
	if GetObjectState("cadeiracastigo") == "clean" then
		PlayCharacterAnimation(self.current_action_character, self.character_messing_animation);
		ShowCharacterStuff(self.current_action_character, "badboy", self.star_x, self.star_y, self.star_layer);
		PutCharacterOnObject(self.current_action_character, self.object_name);
		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";
	else
		FinishCharacterTask(self.current_action_character);
	end
end

function naughtyspot: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 naughtyspot:parent_command()
	RemoveObjectMouseHandler(self.object_name);
	AddCharacterTask(self.parent_chosen, self.object_name, "gotoanddo", self.node_to_do_action);
end

function naughtyspot: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
		HideCharacter(self.parent_chosen);
		if self.parent_chosen == "dad" then
			--PlaySound("working_father", true);
			ChangeObjectState(self.object_dad_warning, "playing");
		else
			--PlaySound("working_mother", true);
			ChangeObjectState(self.object_mom_warning, "playing");
		end		
		
		ChangeObjectState(self.object_name, self.cleaning_state_name);
		SetCharacterState(self.parent_chosen, self.cleaning_state_name);
		self.new_state_start_time = GetCurrentTime();
		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 naughtyspot:cleaning_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		ShowCharacter(self.parent_chosen);
		
		if self.parent_chosen == "dad" then
			--StopSound("working_father");
			PlaySound("task_complete_father", false);
			ChangeObjectState(self.object_dad_warning, "clean");
		else
			--StopSound("working_mother");
			PlaySound("task_complete_mother", false);
			ChangeObjectState(self.object_mom_warning, "clean");
		end
		
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);
		FlipAnimation(self.parent_chosen, self.flipped_animation);
		
		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;
		
		LoadCharacterTask(self.current_action_character, gotoobjectanddo, "cadeiracastigo", "cadeiracastigo");
		
		HideCharacterStuff(self.current_action_character);
		RemoveCharacterOnObject(self.current_action_character);
		FinishCharacterTask(self.current_action_character);
		
		FinishCharacterTask(self.parent_chosen);
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		ChangeObjectState(self.object_name, self.clean_state_name);
		SetCharacterState(self.parent_chosen, self.parent_state_action);
		
		ShowNextTaskIcon("cadeiracastigo");
		ChangeObjectState("cadeiracastigo", "waitingchild");
		
		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
