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

local	indicator_pos_x = -9;
local	indicator_pos_y = -84;

local ObjectTable = GetObjectTable("breakfight");


fight_up_spot = simple_object:new{
	character_cleaning_animation = "doing_stuff_up",
	parent_state_action = "normal",
	anchor_x=83,
	anchor_y=0,
	offset_x=-7,
	offset_y=-18,
	
	cleaning_state_time = 3000;
	
	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,
		
	set_click_enable = false;
	show_cleaning_particle = false;
	
	texture="anim/objects/fight_square/fight_square",
	animation_script="anim/objects/default_animation_script.lua",
	glow_texture="anim/objects/fight_square/fight_square_glow",
	glow_animation_script="anim/objects/default_glow_animation_script.lua",
	
	mask = true;
	maskX = 0;
	maskY = -30;
	maskWidth = 132;
	maskHeight = 130;
	maskAlpha = 0.01;
	
	fight_down_spot_name = "";
	object_dad_warning = "";
	object_mom_warning = "";
	
	complex_object = true;
	
	cleaning_timer_x = 0;
	cleaning_timer_y = -150;
	
	check_teamwork = true;
	finish_teamwork = true;
	teamworktask_name = "stopfight";

	cleaning_state_time = ObjectTable["cleaning_time"],
	stress_inc = ObjectTable["stress_inc"],
	
	discipline_way = "";
	object_dad_disciplining = "";
	object_mom_disciplining = "";
}

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

function fight_up_spot:dirty_init()	
	self.object_is_dirty = true;
	self.click_enable = true;
	PlayObjectAnimation(self.object_name, self.object_dirty_animation);	
	--PlayGlowAnimation(self.object_name, "dirty");
	
	PlayCharacterAnimation(self.current_action_character, "fighting_up");
	FlipAnimation(self.current_action_character, self.flipped_animation);
	
	self.previous_state = GetObjectState(self.object_name);
	
	if not self.parent_is_coming then
		IncludeObjectMouseHandler(self.object_name);
	end
	
	if self.check_teamwork then
		ShowTeamWorkIcon(self.object_name, ShouldShowCharacterTeamWork(self.teamworktask_name));
		--ShowTeamWorkIcon(self.object_name, "mom", ShouldShowCharacterTeamWork("mom", self.teamworktask_name));
	end
	
	--PlaySound("fight", false);
end

function fight_up_spot: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.parent_chosen);
		--[[
		if self.parent_chosen == "dad" then
			--PlaySound("working_father", true);
			ChangeObjectState(self.object_dad_warning, "playing");
		else
			--PlaySound("working_mother", true);
			ChangeObjectState(self.object_mom_warning, "playing");
		end
		--]]
		
		if DecideDisciplineWay(self.parent_chosen) == 0 then
			self.discipline_way = "good";
		else
			self.discipline_way = "bad";
		end	
		
		if self.parent_chosen == "dad" then
			ChangeObjectState(self.object_dad_disciplining, self.discipline_way);
		else
			ChangeObjectState(self.object_mom_disciplining, self.discipline_way);
		end
		
		PlayCharacterAnimation(self.current_action_character, "idle");
		
		StopSound("fight");
		
		ChangeObjectState(self.fight_down_spot_name, "readytodiscipline");
		
		ChangeObjectState(self.object_name, self.cleaning_state_name);
		SetCharacterState(self.parent_chosen, self.cleaning_state_name);
		self.new_state_start_time = GetCurrentTime();
		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 fight_up_spot:cleaning_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		ShowCharacter(self.parent_chosen);
		
		--[[
		StopSound("fight");
		
		if self.parent_chosen == "dad" then
			--StopSound("working_father");
			PlaySound("task_complete_father", false);
			ChangeObjectState(self.object_dad_warning, "clean");
		else
			--StopSound("working_mother");
			PlaySound("task_complete_mother", false);
			ChangeObjectState(self.object_mom_warning, "clean");
		end
		---]]
		if self.parent_chosen == "dad" then
			ChangeObjectState(self.object_dad_disciplining, "transition");
		else
			ChangeObjectState(self.object_mom_disciplining, "transition");
		end
		
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);
		FlipAnimation(self.parent_chosen, self.flipped_animation);
		
		--[[
		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;
		--]]
		
		RemoveCharacterOnObject(self.current_action_character);
		FinishCharacterTask(self.current_action_character);
		
		FinishCharacterTask(self.parent_chosen);
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		ChangeObjectState(self.object_name, self.clean_state_name);
		SetCharacterState(self.parent_chosen, self.parent_state_action);
		
		if self.finish_teamwork then
			FinishTeamWorkTask(self.parent_chosen, self.teamworktask_name, 1);
		end
		
		--[[
		if self.discipline_way == "good" then
			FinishTeamWorkTask(self.parent_chosen, "disciplinesnoyelling");
		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;
		
		ChangeObjectState(self.fight_down_spot_name, "finished");
		
		FinishTeamWorkTask(self.parent_chosen, "disciplines", 1);
		
		if self.discipline_way == "good" then
			FinishTeamWorkTask(self.parent_chosen, "disciplinesnoyelling", 1);
		end
				
		if self.discipline_way == "good" then
			DisciplineKids();
		end
	end
end

function fight_up_spot:clean_youngchild_action(character_name)
	self.current_action_character = character_name;
	if GetObjectState(self.fight_down_spot_name) == "childon" then
		ChangeObjectState(self.object_name, "dirty");
		ChangeObjectState(self.fight_down_spot_name, "dirty");
		PlaySound("fight", false);
	else
		ChangeObjectState(self.object_name, "childon");
	end
	PutCharacterOnObject(self.current_action_character, self.object_name);
end

function fight_up_spot:clean_toddler_action(character_name)
	self.current_action_character = character_name;
	if GetObjectState(self.fight_down_spot_name) == "childon" then
		ChangeObjectState(self.object_name, "dirty");
		ChangeObjectState(self.fight_down_spot_name, "dirty");
		PlaySound("fight", false);
	else
		ChangeObjectState(self.object_name, "childon");
	end
	PutCharacterOnObject(self.current_action_character, self.object_name);
end

function fight_up_spot:clean_schoolaged_action(character_name)
	self.current_action_character = character_name;
	if GetObjectState(self.fight_down_spot_name) == "childon" then
		ChangeObjectState(self.object_name, "dirty");
		ChangeObjectState(self.fight_down_spot_name, "dirty");
		PlaySound("fight", false);
	else
		ChangeObjectState(self.object_name, "childon");
	end
	PutCharacterOnObject(self.current_action_character, self.object_name);
end
