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

local	indicator_pos_x = -11;
local	indicator_pos_y = -39;

local ObjectTable = GetObjectTable("diaper_changer");

diaper_changer = simple_object:new{
	current_action_character="toddler",
	character_cleaning_animation = "doing_stuff_up",
	parent_state_action = "holding_toddler",	
	anchor_x = -20,
	anchor_y = 50,
	offset_x=-4,
	offset_y=-8,
	texture="anim/objects/diaperchanger/trocador",
	indicator_ui_x=indicator_pos_x,
	indicator_ui_y=indicator_pos_y,
	dad_ui_x=indicator_pos_x,
	dad_ui_y=indicator_pos_y-17,
	mom_ui_x=indicator_pos_x,
	mom_ui_y=indicator_pos_y-17,
	
	talco_x = "50";
	talco_y = "-100";
	talco_layer = "-1";
	
	diaper_selected = "fralda";
	
	cleaning_state_time = 3500,
	
	
	show_cleaning_particle = false;
	
	complex_object = true;
	dad_next_task_x = indicator_pos_x+40,
	dad_next_task_y = indicator_pos_y,
	mom_next_task_x = indicator_pos_x+40,
	mom_next_task_y = indicator_pos_y,

	messing_state_time = ObjectTable["messing_time"],
	cleaning_state_time = ObjectTable["cleaning_time"],
	stress_inc = 0,
	
	toddlers = {};
	
	current_toddler = "";
	
	being_used = false;
	
	stuff_name = "diaper";
	
	parent_final_state = "holding_diaper";
	
	character_clean_animation="standing_with_stuff",
	
	is_a_mess = false,
}

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

function diaper_changer:clean_update()
	local dad_state = string.sub(GetCharacterState("dad"), 1, 15);
	local mom_state = string.sub(GetCharacterState("mom"), 1, 15);
	
	if dad_state == self.parent_state_action or
		mom_state == self.parent_state_action then
		ChangeObjectState(self.object_name, "dirty");
	end
end

function diaper_changer: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);
	
	local n = table.getn(self.toddlers);
	local should_clean = false;
	
	for i=1,n do
		--DebugOut(self.toddlers[i]);
		if GetCharacterState(self.parent_chosen) == "holding_"..self.toddlers[i] then			
			should_clean = true;
			self.current_toddler = self.toddlers[i];
		end
	end
	
	if should_clean and
		self.object_is_dirty and 
		not self.being_used and
		GetObjectState(self.diaper_selected) ~= "dirty" then
		if self.parent_chosen == "dad" then
			PlaySound("working_father", true);
		else
			PlaySound("working_mother", true);
		end
		self.being_used = true;
		HideCharacterStuff(self.parent_chosen);	
		PlayCharacterAnimation(self.parent_chosen, self.character_cleaning_animation);
		ShowCharacterStuff(self.parent_chosen, "talco", self.talco_x, self.talco_y, self.talco_layer);
		FlipAnimation(self.parent_chosen, self.flipped_animation);
		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);		
		
		ChangeObjectState(self.current_toddler.."diaperchange", "playing");
		
		self:get_cleaning_state_time();
	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;
		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 diaper_changer:cleaning_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		if self.parent_chosen == "dad" then
			StopSound("working_father");
			PlaySound("task_complete_father", false);
			HideDadNextTaskIcon(self.object_name);
			ShowDadNextTaskIcon("lixeira");
		else
			StopSound("working_mother");
			PlaySound("task_complete_mother", false);
			HideMomNextTaskIcon(self.object_name);
			ShowMomNextTaskIcon("lixeira");
		end
		
		local n = table.getn(self.toddlers);
		local toddler_found = false;
		local i = 1;
		
		while i <= n and not toddler_found do
			if self.toddlers[i] == self.current_toddler then
				toddler_found = true;				
			end
			i = i + 1;
		end
		
		if toddler_found then
			table.remove(self.toddlers, i-1);
		end		
		
		ShowCharacter(self.current_toddler);
		
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);
		HideCharacterStuff(self.parent_chosen);
		--SetCharacterState(self.parent_chosen, "normal");
		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_toddler.."diaperchange", "clean");
		
		ShowCharacterStuff(self.parent_chosen, self.stuff_name, self.talco_x, self.talco_y, self.talco_layer);
		SetCharacterState(self.parent_chosen, self.parent_final_state);
		FinishCharacterTask(self.parent_chosen);
		
		--PlayObjectAnimation(self.diaper_selected, "dirty");
		--ChangeObjectState(self.diaper_selected, "dirty");
		--ShowNextTaskIcon(self.diaper_selected);
		
		FinishCharacterTask(self.current_toddler);
		
		self.current_toddler = "";
		
		--FinishTeamWorkTask(self.parent_chosen, self.teamworktask_name, 1);
		
		if self.check_teamwork then
			ShowTeamWorkIcon(self.object_name, 2);
			--ShowTeamWorkIcon(self.object_name, "mom", false);		
		end
		
		self.cleaning_state_time = self.previous_cleaning_state_time;
		
		if table.getn(self.toddlers) == 0 then
			PlayObjectAnimation(self.object_name, self.object_clean_animation);
			ChangeObjectState(self.object_name, self.clean_state_name);
		else
			PlayObjectAnimation(self.object_name, self.object_dirty_animation);
			ChangeObjectState(self.object_name, self.dirty_state_name);
		end
		
		self.being_used = false;
	end
end

function diaper_changer:clean_toddler_action(character_name)
	table.insert(self.toddlers, character_name);
end

function diaper_changer:messing_toddler_action(character_name)
	table.insert(self.toddlers, character_name);
end

function diaper_changer:cleaning_toddler_action(character_name)
	table.insert(self.toddlers, character_name);
end

function diaper_changer:dirty_toddler_action(character_name)
	table.insert(self.toddlers, character_name);
end

function diaper_changer:flaged_toddler_action(character_name)
	table.insert(self.toddlers, character_name);
end

function diaper_changer:flaging_toddler_action(character_name)
	table.insert(self.toddlers, character_name);
end