local cls = class("webView", G_UIModuleBase)
local tag = cls.getName()

function cls:ctor(...)
  cls.super.ctor(self, ...)
  self._hdl_onWebViewInitialized = handler(self, self.onWebViewInitialized)
  self._hdl_onWebViewMessageEmitted = handler(self, self.onWebViewMessageEmitted)
  self._hdl_onLoadProgressChanged = handler(self, self.onLoadProgressChanged)
  self._hdl_onPageLoadFailed = handler(self, self.onPageLoadFailed)
end

function cls.bind()
  return {
    url = "",
    resolution = 1,
    dragMode = 0,
    scrollingSensitivity = 200,
    dragThreshold = 20,
    loadingView = {
      moduleName = "pages/Common/Modules/loadingView"
    }
  }
end

function cls.methods()
  return {}
end

function cls:open()
  self.modules.loadingView:doHide()
  self._webViewPrefab = self.bindComponents.WebViewPrefab
  if Unity.Application.isMobilePlatform then
    self.bind.resolution = 1
    self.bind.dragMode = 0
  else
    self.bind.resolution = 0.5
    self.bind.dragMode = 1
  end
  self.isShowLoading = true
  if self._webViewPrefab.WebView ~= nil then
    self:registerWebViewEvents()
  else
    self._webViewPrefab:Initialized("+", self._hdl_onWebViewInitialized)
  end
end

function cls:close()
  self.modules.loadingView:doHide()
  self:unregisterWebViewEvents()
end

function cls:setResolution(resolution)
  self.bind.resolution = resolution
end

function cls:showLoading(isShowLoading)
  self.isShowLoading = isShowLoading
end

function cls:registerWebViewEvents()
  if self._webViewPrefab.WebView ~= nil then
    self._webViewPrefab.WebView:MessageEmitted("+", self._hdl_onWebViewMessageEmitted)
    self._webViewPrefab.WebView:LoadProgressChanged("+", self._hdl_onLoadProgressChanged)
    self._webViewPrefab.WebView:PageLoadFailed("+", self._hdl_onPageLoadFailed)
  end
end

function cls:unregisterWebViewEvents()
  if self._webViewPrefab.WebView ~= nil then
    self._webViewPrefab.WebView:MessageEmitted("-", self._hdl_onWebViewMessageEmitted)
    self._webViewPrefab.WebView:LoadProgressChanged("-", self._hdl_onLoadProgressChanged)
    self._webViewPrefab.WebView:PageLoadFailed("-", self._hdl_onPageLoadFailed)
  end
end

function cls:loadBlank()
  self.bind.url = "about:blank"
end

function cls:setUrl(url)
  self.bind.url = url
end

function cls:onWebViewInitialized(sender, args)
  self._webViewPrefab:Initialized("-", self._hdl_onWebViewInitialized)
  self:registerWebViewEvents()
end

function cls:onLoadProgressChanged(sender, args)
  if self.modules.loadingView == nil then
    return
  end
  local changeType = CS.System.Convert.ToInt32(args.Type)
  if changeType == 0 then
    printf(tag, "onLoadProgressChanged: Started")
    if self.isShowLoading then
      self.modules.loadingView:doShow()
    end
  elseif changeType == 1 then
    printf(tag, "onLoadProgressChanged: Finished")
    self.modules.loadingView:doHide()
  end
end

function cls:onPageLoadFailed(sender, args)
  printf(tag, "onPageLoadFailed")
end

function cls:onWebViewMessageEmitted(sender, args)
  printf(tag, "onWebViewMessageEmitted")
  if self._messageHandler then
    self._messageHandler(args.Value)
  end
end

function cls:setMessageHandler(handler)
  self._messageHandler = handler
end

return cls
