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

local	indicator_pos_x = 7;
local	indicator_pos_y = -40;

local	parent_getting_down = true;

sagets_fall_floor = simple_object:new{
	--current_action_character="toddler",
	character_messing_animation="falling",
	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;
	
	complex_object = true;
	
	is_a_mess = false,
	
	number = "0",
}

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

function sagets_fall_floor:clean_mess_action()
	if GetObjectState("asksleep"..self.number) ~= "dirty" and
	GetObjectState("fallonfloor"..self.number) ~= "dirty" and
	GetObjectState("diapersquare"..self.number) ~= "dirty" then
		PlaySound("baby_crying", false);
		PlayCharacterAnimation(self.current_action_character, self.character_messing_animation);
		ShowCharacterStuff(self.current_action_character, "star", 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 sagets_fall_floor:messing_update()
	if not (IsObjectAnimationPlaying(self.object_name)) then
		PlayObjectAnimation(self.object_name, self.object_dirty_animation);
		ChangeObjectState(self.object_name, self.dirty_state_name);
		PlayCharacterAnimation(self.current_action_character, self.character_dirty_animation);
		FlipAnimation(self.current_action_character, self.flip_animation);
	end
end

function sagets_fall_floor:flaging_init()
	RemoveObjectMouseHandler(self.object_name);
	PlayObjectAnimation(self.object_name, self.object_dirty_animation);
	AddCharacterTask(self.parent_chosen, self.object_name, "gotoanddo", self.node_to_do_action);
	ChangeObjectState(self.object_name, self.flaged_state_name);
end

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

--[[
function sagets_fall_floor:flaged_parent_action()
	if self.parent_chosen == "dad" then
		HideDadIcon(self.object_name);
	else
		HideMomIcon(self.object_name);
	end
	if GetCharacterState(self.parent_chosen) == self.parent_state_action then
		PlayCharacterAnimation(self.parent_chosen, "getdown");
		FlipAnimation(self.parent_chosen, self.flip_animation);
		parent_getting_down = true;
		
		PlayObjectAnimation(self.object_name, self.object_cleaning_animation);
		ChangeObjectState(self.object_name, self.cleaning_state_name);
		self.new_state_start_time = GetCurrentTime();
	else
		PlayObjectAnimation(self.object_name, self.object_dirty_animation);
		ChangeObjectState(self.object_name, self.dirty_state_name);
		FinishCharacterTask(self.parent_chosen);
	end
end
--]]

function sagets_fall_floor: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
		PlayCharacterAnimation(self.parent_chosen, "getdown");
		FlipAnimation(self.parent_chosen, self.flip_animation);
		parent_getting_down = true;
		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);
	else
		ShowMiss(self.object_name);
		if self.stress_inc_enabled and not self.no_stress_power then
			AddFutureCharacterStress(self.parent_chosen, -self.stress_inc);
		end
		if self.no_stress_power then
			HideNoStressIcon(self.object_name);
		end
		self.no_stress_power = false;
		--PlayObjectAnimation(self.object_name, self.object_dirty_animation);
		--ChangeObjectState(self.object_name, self.dirty_state_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 sagets_fall_floor:cleaning_update()
	if parent_getting_down then
		if not IsCharacterAnimationPlaying(self.parent_chosen) then
			parent_getting_down = false;
			PlayCharacterAnimation(self.parent_chosen, "getup");
			PlayCharacterAnimation(self.current_action_character, "getup");
		end
	else
		if not IsCharacterAnimationPlaying(self.parent_chosen) and not IsCharacterAnimationPlaying(self.current_action_character) then			
			PlayObjectAnimation(self.object_name, self.object_clean_animation);
			--StopSound("baby_crying");
			if self.parent_chosen == "dad" then
				PlaySound("task_complete_father", false);
			else
				PlaySound("task_complete_mother", false);
			end
			PlayCharacterAnimation(self.parent_chosen, self.character_clean_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
			if self.no_stress_power then
				HideNoStressIcon(self.object_name);
			end
			self.no_stress_power = false;
			FinishCharacterTask(self.parent_chosen);
			
			HideCharacterStuff(self.current_action_character);
			RemoveCharacterOnObject(self.current_action_character);
			FinishCharacterTask(self.current_action_character);
			SetCharacterState(self.parent_chosen, "normal");
			
			self.cleaning_state_time = self.previous_cleaning_state_time;
			ChangeObjectState(self.object_name, self.clean_state_name);
		end		
	end
end