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

local	indicator_pos_x = -1;
local	indicator_pos_y = -66;

local	toddler_x = "20";
local	toddler_y = "-50";
local	toddler_layer = "1";

local ObjectTable = GetObjectTable("carrytoddler");

sagets_carry_toddler = simple_object:new{
	--current_action_character="toddler",
	character_messing_animation="standing_crying",
	character_dirty_animation="standing_crying",
	character_cleaning_animation="doing_bow_down",
	character_clean_animation="standing_with_stuff",
	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,
	messing_state_time = 0,
	cleaning_state_time = 200,
	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",
	
	--toddler_destination = "trocador";
	toddler_destination = {};
	show_feedback_animation = false;
	
	set_click_enable = false;
	
	stress_inc_enabled = false;
	
	mask = true;
	maskX = 0;
	maskY = -30;
	maskWidth = 92;
	maskHeight = 90;
	maskAlpha = 0.01;
	show_cleaning_particle = false;
	
	sound_of_work_enable = false;
	
	stuff_name = "";
	
	complex_object = true;

	messing_state_time = ObjectTable["messing_time"],
	cleaning_state_time = ObjectTable["cleaning_time"],
	
	is_a_mess = false,
	
	number = "0",
}

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

function sagets_carry_toddler:clean_mess_action()
	if GetObjectState("asksleep"..self.number) ~= "dirty" and
	GetObjectState("fallonfloor"..self.number) ~= "dirty" and
	GetObjectState("diapersquare"..self.number) ~= "dirty" then
		PlayCharacterAnimation(self.current_action_character, self.character_messing_animation);
		FlipAnimation(self.current_action_character, self.flipped_animation);
		PlayObjectAnimation(self.object_name, self.object_messing_animation);
		ChangeObjectState(self.object_name, self.messing_state_name);
		self.new_state_start_time = GetCurrentTime();
		if self.sound_of_messing ~= "" then
			PlaySound(self.sound_of_messing, true);
		end	
		self.previous_state = "messing";
	else
		FinishCharacterTask(self.current_action_character);
	end
end

function sagets_carry_toddler: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);
		PutCharacterOnObject(self.current_action_character, self.object_name);
		ShowCharacterStuff(self.current_action_character, self.stuff_name, toddler_x, toddler_y, toddler_layer);
		PlaySound("baby_crying", false);
	end
end

function sagets_carry_toddler:cleaning_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		--StopSound("baby_crying");
		if self.parent_chosen == "dad" then			
			PlaySound("task_complete_father", false);
			ShowDadNextTaskIcon(self.toddler_destination[self.current_action_character]);
		else
			PlaySound("task_complete_mother", false);
			ShowMomNextTaskIcon(self.toddler_destination[self.current_action_character]);
		end
		RemoveCharacterOnObject(self.current_action_character);
		HideCharacter(self.current_action_character);
		
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);	
		ShowCharacterStuff(self.parent_chosen, self.family..self.current_action_character, toddler_x, toddler_y, toddler_layer);
		SetCharacterState(self.parent_chosen, "holding_tosleep_"..self.current_action_character);
		--AddCharacterStress(self.parent_chosen, self.stress_inc);
		FinishCharacterTask(self.parent_chosen);
		--debugout("Loading**********************************************");
		LoadCharacterTask(self.current_action_character, teleporttoobjectanddo, self.toddler_destination[self.current_action_character], self.toddler_destination[self.current_action_character]);
		
		HideCharacterStuff(self.current_action_character);
		
		PlayObjectAnimation(self.object_name, self.object_clean_animation);		
		ChangeObjectState(self.object_name, self.clean_state_name);		
		FinishCharacterTask(self.current_action_character);
		
		if self.no_stress_power then
			HideNoStressIcon(self.object_name);
		end
		self.no_stress_power = false;
		
		PlayObjectAnimation(self.toddler_destination[self.current_action_character], "dirty");
		ChangeObjectState(self.toddler_destination[self.current_action_character], "dirty");
		
		self.cleaning_state_time = self.previous_cleaning_state_time;
	end
end
