local this = class("moduleVideo", G_UIModuleBase)
local CriWareStatus = CS.CriWare.CriMana.Player.Status

function this.bind()
  return {go_content = false, go_btnSkip = false}
end

function this.methods()
  return {
    onClick_skip = function(self)
      self:playVideoEnd()
    end
  }
end

function this:close()
  L_TimerManager:clearTimer(self)
end

function this:playVideo(moviePath, startCallback, endCallback)
  self._init = true
  self._moviePath = moviePath
  self._endCallback = endCallback
  self._startCallback = startCallback
  printf("\230\146\173\230\148\190\232\167\134\233\162\145", self._moviePath)
  local isEmpty = string.isEmpty(self._moviePath)
  self.bind.go_btnSkip = false
  self.bind.go_content = not isEmpty
  if isEmpty then
    if self._endCallback then
      self._endCallback()
    end
    return
  else
    local isWatched = L_CommonUtil.getLocalValue(self._moviePath) ~= nil
    if isWatched then
      L_TimerManager:newOrResetTimer(self, "showSkip", function()
        self.bind.go_btnSkip = true
      end, 1)
    else
      L_CommonUtil.setLocalValue(self._moviePath, 1)
    end
  end
  self._videoComponent = self.bindComponents.movieController_moduleVideo
  self._videoComponent.player.statusChangeCallback = handler(self, self.statusChangeCallback)
  self._videoComponent.moviePath = self._moviePath
  self._videoComponent.playOnStart = true
end

function this:statusChangeCallback(status)
  if status == CriWareStatus.Error then
    error("\230\146\173\230\148\190\229\135\186\233\148\153,\232\175\183\230\163\128\230\159\165\232\183\175\229\190\132\230\152\175\229\144\166\230\173\163\231\161\174")
    self:playVideoEnd()
  elseif status == CriWareStatus.PlayEnd then
    self:playVideoEnd()
  elseif status == CriWareStatus.Playing and self._init then
    if self._startCallback then
      self._startCallback(self._videoComponent.Duration)
    end
    self._init = false
  end
end

function this:playVideoEnd()
  if self._endCallback then
    self._endCallback()
  end
  self.bind.go_content = false
  self.bind.go_btnSkip = false
end

return this
