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

local	indicator_pos_x = -30;
local	indicator_pos_y = -68;

local ObjectTable = GetObjectTable("leakingfaucet");

faucet = simple_object:new{
	--current_action_character="schoolaged",
	anchor_x=0,
	anchor_y=40,
	offset_x = -3,
	offset_y = -4,
	texture="anim/objects/bathroomsink/bathroomsink",
	animation_script="anim/objects/default_animation_script.lua",
	glow_texture="anim/objects/bathroomsink/bathroomsink_glow",
	glow_animation_script="anim/objects/default_glow_animation_script.lua",
	--animated=false,
	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,
	
	character_messing_animation = "doing_stuff_up",
	character_cleaning_animation = "doing_stuff_up",
	
	--sound_of_messed = "faucet_running",

	messing_state_time = ObjectTable["messing_time"],
	cleaning_state_time = ObjectTable["cleaning_time"],
	stress_inc = ObjectTable["stress_inc"],
	
	show_cleaning_particle = false;
}

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

function faucet:cancel()
	DebugOut(""..self.object_name..":cancel");	
	DebugOut(""..self.object_name..":previous:"..self.previous_state);	
	DebugOut(""..self.object_name..":parent:"..self.parent_chosen);	
	
	self.parent_is_coming = false;
	--self.click_enable = self.set_click_enable;
	--IncludeObjectMouseHandler(self.object_name);	
	
	if GetCharacterState(self.parent_chosen) ~= "cleaning" and
		GetCharacterState(self.parent_chosen) ~= "sit" and
		GetCharacterState(self.parent_chosen) ~= "sit_outside" and 
		GetCharacterState(self.parent_chosen) ~= "sit_to_play"	then
		i, j = string.find(GetCharacterState(self.parent_chosen), "holding");
		if i ~= 1 then
			PlayCharacterAnimation(self.parent_chosen, "idle");
			SetCharacterState(self.parent_chosen, "normal");
		else			
			PlayCharacterAnimation(self.parent_chosen, "standing_with_stuff");
			ShowCharacterStuff(self.parent_chosen, GetCharacterStuff(self.parent_chosen), "0", "0", "0");
		end		
	end
		
	if self.previous_state == "dirty" then
		if self.stress_inc_enabled and not self.no_stress_power then
			AddFutureCharacterStress(self.parent_chosen, -self.stress_inc);
		end
	end
	
	if self.no_stress_power then
		HideNoStressIcon(self.object_name);
	end
	
	self.no_stress_power = false;
	
	if self.parent_chosen == "dad" then
		HideDadIcon(self.object_name);
	else
		HideMomIcon(self.object_name);
	end
	
	RemoveObjectFromClickedList(self.object_name);
	if self.previous_state ~= "messing" then
		if self.previous_state == "dirty" then
			self.object_is_dirty = true;
			PlayObjectAnimation(self.object_name, self.object_dirty_animation);	
			
			self.click_enable = true;
			IncludeObjectMouseHandler(self.object_name);
		else
			ChangeObjectState(self.object_name, self.previous_state);
		end
	end
	--self.parent_chosen = "";
end

function faucet:clean_mess_action()
	PlaySound("faucet_running", false);
	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";
end

function faucet:dirty_init()
	DebugOut(""..self.object_name..":dirty_init");
	self.object_is_dirty = true;
	PlayObjectAnimation(self.object_name, self.object_dirty_animation);	
	--PlayGlowAnimation(self.object_name, "dirty");
	
	self.previous_state = GetObjectState(self.object_name);
	
	if not self.parent_is_coming then
		self.click_enable = true;
		IncludeObjectMouseHandler(self.object_name);
	else
		self.click_enable = false;
		PlayGlowAnimation(self.object_name, "dirty_click");
		if self.stress_inc_enabled and not self.no_stress_power then
			AddFutureCharacterStress(self.parent_chosen, self.stress_inc);
		end	
	end
	
	if self.check_teamwork then
		ShowTeamWorkIcon(self.object_name, ShouldShowCharacterTeamWork(self.teamworktask_name));
	end
	
	ShowParticle(self.object_name, "effects/water.lua", "1", "-21");
end

function faucet:cleaning_update()
		if self.cleaning_timer_enabled then
			SetCleaningTimerProgress(self.object_name, (GetCurrentTime() - self.new_state_start_time) / self.cleaning_state_time);
		end
		if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
			StopSound("faucet_running");
			
			if self.cleaning_timer_enabled then
				SetCleaningTimerVisible(self.object_name, false);
			end
			
			if self.parent_chosen == "dad" then
				if self.sound_of_work_enable then
					StopSound("working_father");
				end
				PlaySound("task_complete_father", false);
			else
				if self.sound_of_work_enable then
					StopSound("working_mother");
				end
				PlaySound("task_complete_mother", false);
			end
			
			PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);
			FlipAnimation(self.parent_chosen, self.flipped_animation);
			--HideParticle(self.object_name, "powder");
			HideParticle(self.object_name, "powder");
			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;
			FinishCharacterTask(self.parent_chosen);
			PlayObjectAnimation(self.object_name, self.object_clean_animation);
			ChangeObjectState(self.object_name, self.clean_state_name);
			SetCharacterState(self.parent_chosen, "normal");
			
			if self.finish_teamwork then
				FinishTeamWorkTask(self.parent_chosen, self.teamworktask_name, 1);
			end
			
			if self.check_teamwork then
				ShowTeamWorkIcon(self.object_name, 2);
				--ShowTeamWorkIcon(self.object_name, "mom", false);		
			end
			
			if self.is_a_mess then
				FinishTeamWorkTask(self.parent_chosen, "messes", 1);
			end
			
			self.cleaning_state_time = self.previous_cleaning_state_time;
		end
	--end
end
