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

local	indicator_pos_x = 0;
local	indicator_pos_y = -80;

local ObjectTable = GetObjectTable("diaper");

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

sagets_toddler_diaper_change = 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 = 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",
	
	diaper_changed_selected = "trocador",
	
	diaper_x = "10",
	diaper_y = "-80",
	diaper_layer = "1",
	
	set_click_enable = false;
	
	check_teamwork = true;
	teamworktask_name = "changediaper";
	
	mask = true;
	maskX = 0;
	maskY = -30;
	maskWidth = 92;
	maskHeight = 90;
	maskAlpha = 0.01;
	
	show_cleaning_particle = false;
	
	stress_inc_enabled = false;
	
	complex_object = true;
	
	diaper_square_list = {};

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

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

--[[
function sagets_toddler_diaper_change:clean_mess_action()
	local n = table.getn(self.diaper_square_list);
	local diaper_change_on_course = false;
	
	for i = 1 , n do
		if GetObjectState(self.diaper_square_list[i]) == "dirty" then
			diaper_change_on_course = true;			
		end
	end
	
	if GetObjectState("trocador") ~= "dirty" and
		GetObjectState("fralda") ~= "dirty" and 
		GetObjectState("lixeira") ~= "dirty" and
		not diaper_change_on_course 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
	else
		FinishCharacterTask(self.current_action_character);
	end
end
--]]

function sagets_toddler_diaper_change: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_toddler_diaper_change:messing_update()
	ShowCharacterStuff(self.current_action_character, "diaper_thought", self.diaper_x, self.diaper_y, self.diaper_layer);
	PutCharacterOnObject(self.current_action_character, self.object_name);
	PlayObjectAnimation(self.object_name, self.object_dirty_animation);
	ChangeObjectState(self.object_name, self.dirty_state_name);
	PlaySound("baby_crying", false);
end

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

function sagets_toddler_diaper_change:cleaning_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		--StopSound("baby_crying");
		if self.parent_chosen == "dad" then
			StopSound("working_father");
			PlaySound("task_complete_father", false);
			ShowDadNextTaskIcon(self.diaper_changed_selected);
		else
			StopSound("working_mother");
			PlaySound("task_complete_mother", false);
			ShowMomNextTaskIcon(self.diaper_changed_selected);
		end
		HideCharacter(self.current_action_character);
		HideCharacterStuff(self.current_action_character);
		RemoveCharacterOnObject(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_"..self.current_action_character);
		--AddCharacterStress(self.parent_chosen, self.stress_inc);
		FinishCharacterTask(self.parent_chosen);
		
		if self.no_stress_power then
			HideNoStressIcon(self.object_name);
		end
		self.no_stress_power = false;
		
		--DebugOut("Loading**********************************************");
		LoadCharacterTask(self.current_action_character, teleporttoobjectanddo, self.diaper_changed_selected, self.diaper_changed_selected);
		
		PlayObjectAnimation(self.object_name, self.object_clean_animation);		
		ChangeObjectState(self.object_name, self.clean_state_name);		
		FinishCharacterTask(self.current_action_character);
		
		self.cleaning_state_time = self.previous_cleaning_state_time;
	end
end
