  ----------------------------------------------------------------------------------------
-- tasks.lua
----------------------------------------------------------------------------------------
-- Game

NewTask("tSleep")

function tSleep:Start()
    self.time = Time:GetTime_milisecond() + self[1] --* 1000
end

function tSleep:Test()
    if Time:GetTime_milisecond() > self.time then
        return true
    end
end


NewTask("tSystemSleep")

function tSystemSleep:Start()
    self.time = Time:GetAbsoluteTime() + self[1] * 1000
end

function tSystemSleep:Test()
    if Time:GetAbsoluteTime() > self.time then
        return true
    end
end

----------------------------------------------------------------------------------------
-- Fade

NewTask("tFadeIn")

function tFadeIn:Start()
    if not self[1] then
        self[1] = Config.fade_in_time
    end

    Game:FadeIn(self[1])
end

function tFadeIn:Test()
    return not Game:IsFade()
end


NewTask("tFadeOut")

function tFadeOut:Start()
    if not self[1] then
        self[1] = Config.fade_out_time
    end

    Game:FadeOut(self[1])
end

function tFadeOut:Test()
    return not Game:IsFade()
end

----------------------------------------------------------------------------------------
-- Inventory 

NewTask("tTake")

function tTake:Start()
    if not self[2] then
        self[2] = 1
    end

    MystInventory:GetActive():Take(self[1], self[2])
end

NewTask("tDrop")

function tDrop:Start()
    if not self[2] then
        self[2] = 1
    end

    MystInventory:GetActive():Drop(self[1], self[2])
end

NewTask("tDelete")

function tDelete:Start()
    if not self[2] then
        self[2] = 1
    end

	MystInventory:GetActive():ReleaseItem()
	MystInventory:GetActive():Drop(self[1], self[2], -1)
end

NewTask("tTakeBigPanel")

action["Item_ShowBigPanel"] = function (r, q, i)
  Widgets.Inventory.ShowBigPanel(i)
end
  
function tTakeBigPanel:Start()

  if not count then
    count = 1
  end
  q = Cmd(tTake(self[1], count))
  q(tSleep(1))
  q(tAction("Item_ShowBigPanel", self[1]))
  q:SetHolded()
  PlaySound("snd_fx_get_item")
  self.Queue = q
end

function tTakeBigPanel:Test()
  return not self.Queue:IsActive()
end
----------------------------------------------------------------------------------------
-- Sound 

NewTask("tSound")

function tSound:Start()
	 Audio:Play(self[1])
end
----------------------------------------------------------------------------------------
-- Movie 


NewTask("tMovieFadeless")

function tMovieFadeless:Start()
    Video:PlayMovie(self[1])
end

function tMovieFadeless:Test()
    return states.sys_video_play ~= self[1]
end


NewTask("tMovie")

function tMovie:Start()
    if (type(self[2]) == "boolean" and self[2]) or self[2] == nil then
        self[2] = Config.fade_out_time
    end

    if (type(self[3]) == "boolean" and self[3]) or self[3] == nil then
        self[3] = Config.fade_in_time
    end

    self.cmd = Cmd()

    if self[2] then
        self.cmd(tFadeOut(self[2]))
    end

    self.cmd(tMovieFadeless(self[1]))

    if self[3] then
        self.cmd(tFadeIn(self[3]))
    end
end

function tMovie:Test()
    return not self.cmd:IsActive()
end



NewTask("tSprMovie")

function tSprMovie:Start()
	 Video:Play(self[1],false)
end

function tSprMovie:Test()
    return Video:IsDone(self[1])
end

NewTask("tSprMovieSeek")

function tSprMovieSeek:Start()
	 Video:Pause(self[1])
	 Video:Seek(self[1], self[2])
end

----------------------------------------------------------------------------------------
-- Sprite 

NewTask("tSprVisible")

function tSprVisible:Start()
	 scene_nodes[self[1]]:SetVisible(self[2])
end

----------------------------------------------------------------------------------------
-- Scene 

NewTask("tGoToFadeless")

function tGoToFadeless:Start()
    Scene:ChangeCamera(self[1])
end


NewTask("tGoTo")

function tGoTo:Start()
	states["sys_block_esc"] = 1
    if (type(self[2]) == "boolean" and self[2]) or self[2] == nil then
        self[2] = Config.fade_out_time
	end

    if (type(self[3]) == "boolean" and self[3]) or self[3] == nil then
        self[3] = Config.fade_in_time
    end
	
    self.cmd = Cmd()

    if self[2] then
        self.cmd(tFadeOut(self[2]))
    end

    self.cmd(tGoToFadeless(self[1]))

    if self[3] then
        self.cmd(tFadeIn(self[3]))
    end
end

function tGoTo:Test()
	local isFinish = not self.cmd:IsActive()
	if isFinish then 
		states["sys_block_esc"] = 0
	end
    return isFinish
end

NewTask("tProgress")

function tProgress:Start()
    GoToProgress(self[1])
end
----------------------------------------------------------------------------------------

NewTask("tWhile")

function tWhile:Test()
  if self[1] == nil then return  true end
  
	if not self[1]() then
		return true
	end
  
  if self[2] == nil then return  true end
	self[2]()
end

NewTask("tLuaAction")

function tLuaAction:Start()
    self[1]()
end

NewTask("tTalk")
function tTalk:Start()
    self.CommentName = self[1]
    if self[2] ~= nil then
        if states[self.CommentName] >=  self[2] then
            self.dontshow = true
            return
        end
    end
    MystDialog:Talk(self.CommentName)
end

function tTalk:Test()
    if self.dontshow then
        return true
    end
    return MystDialog:IsDialogFinished(self.CommentName)
end

NewTask("tDialog")

function tDialog:Start()
    self.DialogName = self[1]
    MystDialog:Start(self.DialogName)
end

function tDialog:Test()
    return MystDialog:IsDialogFinished(self.DialogName)
end

NewTask("tRotate")

function tRotate:Start()
    -- self[1] - node
    -- self[2] - start angle [degree] or nil (current angle)
    -- self[3] - 1 turn time [s]
    -- self[4] - rotate angle [degree]
    -- self[5] - side [-1, 1]
    
    if self[2] then
        self.angle = self[2]
    else
        self.angle = self[1]:GetAngle():valueDegrees()
    end
    
    self.angleDelta = 0
    self.startTime = Time:GetTime() / 1000000
end

function tRotate:Test()
    self[1]:SetAngle(Ogre.Degree(self.angle + self.angleDelta * self[5]))
    local t = Time:GetTime() / 1000000
    local td = t - self.startTime
    self.angleDelta = 360 * td / self[3]
    if self.angleDelta > self[4] then
        self.angleDelta = self[4]
        self[1]:SetAngle(Ogre.Degree(self.angle + self.angleDelta * self[5]))
        return true
    end
end

NewTask("tSlide")

function tSlide:Start()
    -- self[1] - node
    -- self[2] - start x
    -- self[3] - start y
    -- self[4] - end x
    -- self[5] - end y
    -- self[6] - slide time
    self.ltime = Time:GetTime()
end

function tSlide:Test()
    local delta = Time:GetTime() - self.ltime
    local a = delta / 1000000.0 / self[4]
    local px = self[2] + (self[4] - self[2]) * StepSin(a)
    local py = self[3] + (self[5] - self[3]) * StepSin(a)
    self[1]:SetPosition(Ogre.Vector2(px, py))
    if a >= 1.0 then
        return true
    end
end


NewTask("tSetState")

function tSetState:Start()
  states[self[1]] = self[2]
end

NewTask("tWaitForStateChange")

function tWaitForStateChange:Start()
  self.state_first = states[self[1]]
end

function tWaitForStateChange:Test()
  return self.state_first ~= states[self[1]]
end

NewTask("tCreditsOpen")

function tCreditsOpen:Start()
  s.sys_dont_show_menu_widget = 1 
  OpenMainMenu()
  OpenCredits()
end



NewTask("tWaitForBigPanel")

function tWaitForBigPanel:Start()
    self:GetCmd():SetHolded(false)
end

function tWaitForBigPanel:Test()
  return UI:WidgetExists("item_big") ~= true
end

----------------------------------------------------------------------------------------


function action.PlaySound(result, cmd, sound)
    Audio:Play(sound)
end


function action.GoToProgress(result, cmd, progress)
    GoToProgress(progress)
end

function action.StartHO(result, cmd, hobject)
    scene_nodes[hobject]:StartHO()
end

