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

local	indicator_pos_x = 0;
local	indicator_pos_y = -80;

local ObjectTable = GetObjectTable("timetoplaywithdog");

play_with_dog = simple_object:new{
	anchor_x=0,
	anchor_y=20,
	
	character_cleaning_animation = "doing_bow_down",
	
	--object_cleaning_animation="_walking_with_dog",
	
	indicator_ui_x=indicator_pos_x,
	indicator_ui_y=indicator_pos_y,
	dad_ui_x=indicator_pos_x-30,
	dad_ui_y=indicator_pos_y,
	mom_ui_x=indicator_pos_x-30,
	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",
	
	set_click_enable = false;
		
	mask = true;
	maskX = 0;
	maskY = -30;
	maskWidth = 92;
	maskHeight = 90;
	maskAlpha = 0.01;
	
	show_cleaning_particle = false;
	
	stress_inc_enabled = false;
	
	action_node = "";
	
	complex_object = true;
	
	check_teamwork = true;
	finish_teamwork = true;
	teamworktask_name = "playwithdog";

	cleaning_state_time = ObjectTable["cleaning_time"],
	stress_inc = ObjectTable["stress_inc"],
}

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

function play_with_dog:messing_update()
	PlaySound("barking", false);
	PutCharacterOnObject(self.current_action_character, self.object_name);
	PlayCharacterAnimation(self.current_action_character, self.character_dirty_animation);
	FlipAnimation(self.current_action_character, self.flipped_animation);
	ChangeObjectState(self.object_name, self.dirty_state_name);
	ShowCharacterStuff(self.current_action_character, "play_with_dog", "0", "0", "0");
end	

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

function play_with_dog:parent_action()	
	self.parent_is_coming = false;
	DebugOut("simple_object:parent_action:"..self.object_name);
	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
		HideCharacter(self.current_action_character);
		HideCharacter(self.parent_chosen);
		
		HideCharacterStuff(self.current_action_character);
		
		if self.parent_chosen == "dad" then
			HideDadSelector();
		else
			HideMomSelector();
		end
		
		--PlayCharacterAnimation(self.parent_chosen, self.character_cleaning_animation);
		--FlipAnimation(self.parent_chosen, self.flipped_animation);
		
		ChangeObjectState(self.parent_chosen.."playingdog", "playing");
		ChangeObjectState(self.current_action_character.."playing", "playing");
		
		ChangeObjectState(self.object_name, self.cleaning_state_name);
		SetCharacterState(self.parent_chosen, self.cleaning_state_name);
		
		PlayGlowAnimation(self.object_name, "clean");
		
	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 play_with_dog:cleaning_update()
	if GetObjectState(self.parent_chosen.."playingdog") == "clean" then
		if self.parent_chosen == "dad" then
			PlaySound("task_complete_father", false);
		else
			PlaySound("task_complete_mother", false);
		end
		ShowCharacter(self.current_action_character);
		ShowCharacter(self.parent_chosen);
		
		if self.parent_chosen == "dad" then
			ShowDadSelector();
		else
			ShowMomSelector();
		end
		
		RemoveCharacterOnObject(self.current_action_character);
		
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);	
		--ShowCharacterStuff(self.parent_chosen, "toddler", toddler_x, toddler_y, toddler_layer);
		--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;
		
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		ChangeObjectState(self.object_name, self.clean_state_name);
		
		FinishCharacterTask(self.current_action_character);
		
		SetCharacterState(self.parent_chosen, self.parent_state_action);
		
		
		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
		
		self.cleaning_state_time = self.previous_cleaning_state_time;
	end
end
