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

local	indicator_pos_x = -5;
local	indicator_pos_y = -20;

local ObjectTable = GetObjectTable("timetocook");

stove1 = simple_object:new{

	character_cleaning_animation = "doing_stuff_up",
	parent_state_action = "holding_food",
	anchor_x=83,
	anchor_y=135,
	offset_x=-9,
	offset_y=-8,
	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,
	
	sleeping_symbol_x = "0";
	sleeping_symbol_y = "0";
	sleeping_symbol_layer = "2";
	
	cleaning_state_time = 5000;
	
	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,
	show_cleaning_particle = false;
	
	complex_object = true;
	dad_next_task_x = indicator_pos_x,
	dad_next_task_y = indicator_pos_y-30,
	mom_next_task_x = indicator_pos_x,
	mom_next_task_y = indicator_pos_y-30,
	
	stress_inc_enabled = false;
	
	cleaning_timer_x = 0;
	cleaning_timer_y = -150;
	
	stuff_name = "food_cooked";
	stuff_x = "20";
	stuff_y = "-50";
	stuff_layer = "1";
	
	parent_final_state = "holding_cooked_food";
	next_object_selected = "mesajantar2";

	cleaning_state_time = ObjectTable["cleaning_time"];
}

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

--[[
function stove1:dirty_mouse_on()
	if not self.mouse_on then
		self.mouse_on = true;
		if GetCharacterState("dad") == self.parent_state_action then
			ShowDadIcon(self.object_name);
		end
		if GetCharacterState("mom") == self.parent_state_action then
			ShowMomIcon(self.object_name);
		end
		PlayObjectAnimation(self.object_name, self.object_dirty_mouse_on_animation);
	end
end
--]]

function stove1: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);
	if GetCharacterState(self.parent_chosen) == self.parent_state_action and self.object_is_dirty then
		if self.parent_chosen == "dad" then
			--PlaySound("working_father", true);
			HideDadNextTaskIcon(self.object_name);
		else
			--PlaySound("working_mother", true);
			HideMomNextTaskIcon(self.object_name);
		end
		
		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;
		
		HideCharacterStuff(self.parent_chosen);
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);	
		SetCharacterState(self.parent_chosen, "normal");
		--AddCharacterStress(self.parent_chosen, self.stress_inc);
		--FinishCharacterTask(self.parent_chosen);
		
		PlayGlowAnimation(self.object_name, "clean");
		PlayObjectAnimation(self.object_name, "cooking");
		PlayObjectAnimation("fogao2", "cooking");
		ChangeObjectState(self.object_name, self.cleaning_state_name);
		
		
		PlaySound("cooking", true);
		
		self.new_state_start_time = GetCurrentTime();
		--SetCharacterState(self.parent_chosen, self.cleaning_state_name);
		
		self:get_cleaning_state_time();
		
		SetCleaningTimerVisible(self.object_name, true);
	else
		ShowMiss(self.object_name);
		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 stove1:cleaning_update()
	SetCleaningTimerProgress(self.object_name, (GetCurrentTime() - self.new_state_start_time) / self.cleaning_state_time);
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		StopSound("cooking");
		SetCleaningTimerVisible(self.object_name, false);
		if self.parent_chosen == "dad" then
			--StopSound("working_father");
			PlaySound("task_complete_father", false);
			--HideDadNextTaskIcon(self.object_name);
		else
			--StopSound("working_mother", true);
			PlaySound("task_complete_mother", false);
			--HideMomNextTaskIcon(self.object_name);
		end
		
		if self.parent_chosen == "dad" then
			ShowDadNextTaskIcon(self.next_object_selected);
		else
			ShowMomNextTaskIcon(self.next_object_selected);
		end
		
		self.click_enable = false;	
		
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);
		ShowCharacterStuff(self.parent_chosen, self.stuff_name, self.stuff_x, self.stuff_y, self.stuff_layer);
		SetCharacterState(self.parent_chosen, self.parent_final_state);
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		FinishCharacterTask(self.parent_chosen);
		ChangeObjectState(self.object_name, "clean");
		PlayObjectAnimation(self.next_object_selected, "dirty");
		ChangeObjectState(self.next_object_selected, "dirty");
		PlayObjectAnimation("fogao2", "clean");
		FinishTeamWorkTask(self.parent_chosen, "makesdinner", 1);
		--PlayObjectAnimation("fogao2", "dirty");
		--ChangeObjectState("fogao2", "dirty");
		--ShowNextTaskIcon("fogao2");
		
		--SetCharacterState(self.parent_chosen, "normal");
		
		self.cleaning_state_time = self.previous_cleaning_state_time;
	end
end