local this = class("pagePhotoEdit", G_UIPageBase)
local _tabInfo = {
  {
    tabId = 2,
    txt_name = "\229\137\170\232\163\129",
    groupName = "resize"
  },
  {
    tabId = 3,
    txt_name = "\232\180\180\231\186\184",
    groupName = "stamp"
  },
  {
    tabId = 4,
    txt_name = "\230\150\135\229\173\151",
    groupName = "text"
  }
}

function this:created(...)
  this.super.created(self, ...)
end

function this.bind()
  return {
    commonTop = L_Const.ModuleInfo.CommonTop,
    tabList = {
      moduleName = "pages/photoEdit/cellPhotoEditTab"
    },
    curTabID = 0,
    group = this.registBindGroup(),
    groupName = ""
  }
end

function this.methods()
  return {
    onClick_save = function(self)
      self:save()
    end,
    onTabSelect = function(self, tabId)
      self.bind.curTabID = tabId
      self.bind.groupName = tostring(tabId)
      self:refreshSubModule(tabId)
    end,
    onDragImage = function(self, screenPos)
      if self.bind.curTabID == 2 then
        local subModule = self.modules.group["2"]
        subModule:onDragImage(screenPos, self.bindComponents.img_photoRect)
      end
    end,
    onDragImageBegin = function(self, screenPos)
      if self.bind.curTabID == 2 then
        local subModule = self.modules.group["2"]
        subModule:onDragImageBegin(screenPos)
      end
    end,
    onDragImageEnd = function(self, screenPos)
      if self.bind.curTabID == 2 then
        local subModule = self.modules.group["2"]
        subModule:onDragImageEnd()
      end
    end,
    group = {
      onClick_confirm = function(self)
        self.bind.curTabID = 3
      end
    }
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self.rt = options.rt
  self:setImg()
  self:autoFit()
  self:initTop()
  self:initTab()
  self.onEditComplete = options.onEditComplete
end

function this:initTop()
  local data = {
    name = "\231\133\167\231\137\135\231\188\150\232\190\145",
    mainBtn = false,
    closeCurPage = function()
      L_UI:close(self.pageName)
    end
  }
  self.modules.commonTop:initModule(data)
end

function this:initTab()
  for i, v in pairs(_tabInfo) do
    v.tabGroup = self.bindComponents.tabGroup
  end
  self.bind.tabList:clear()
  self.bind.tabList:insert_array(_tabInfo)
  self.bind.curTabID = 2
end

function this.registBindGroup()
  local tab = {
    type = "toggleModule"
  }
  for key, value in pairs(_tabInfo) do
    local groupId = value.tabId
    local cell = {
      assetName = "UI/Pages/PhotoEdit/Group/modulePhotoEdit_" .. value.groupName,
      moduleName = "pages/photoEdit/group/modulePhotoEdit_" .. value.groupName
    }
    tab[tostring(groupId)] = cell
  end
  return tab
end

function this:refreshSubModule(id)
  local subModule = self.modules.group[tostring(id)]
  if not subModule.isInit then
    subModule:initParam({
      rt = self.rt,
      maskRect = self.bindComponents.maskRect,
      imgRoot = self.bindComponents.imgRoot,
      photoRect = self.bindComponents.img_photoRect,
      photoMask = self.bindComponents.photoMask
    })
    if id == 2 then
      function subModule.onClick_confirmCallBack()
        self.bind.curTabID = 3
      end
    end
    if id == 4 then
      function subModule.onClick_deleteCallBack()
        self.bind.curTabID = 3
      end
    end
  end
end

function this:setImg()
  local mat = C_UnityObject.Instantiate(self.bindComponents.imgPreview.material)
  mat:EnableKeyword("FIX_TO_GAMMA")
  self.bindComponents.img_photo.material = mat
  self.bindComponents.img_photo.texture = self.rt
end

function this:autoFit()
  local parentRect = self.bindComponents.imgContainer
  local parentWidth = parentRect.rect.size.x
  local parentHeight = parentRect.rect.size.y
  local imgWidth = self.rt.width
  local imgHeight = self.rt.height
  local ratio = imgWidth / imgHeight
  local targetWidth, targetHeight
  if ratio < parentWidth / parentHeight then
    targetHeight = parentHeight
    targetWidth = targetHeight * ratio
  else
    targetWidth = parentWidth
    targetHeight = targetWidth / ratio
  end
  local size = C_Vector2(targetWidth, targetHeight)
  self.bindComponents.imgRoot.sizeDelta = size
  self.bindComponents.photoMask.gameObject:GetComponent(typeof(C_RectTransform)).sizeDelta = size
  self.bindComponents.img_photo.gameObject:GetComponent(typeof(C_RectTransform)).sizeDelta = size
end

function this:show(options)
end

function this:hide()
end

function this:close(options)
  if self.captureCanvas then
    C_GameObject.Destroy(self.captureCanvas)
  end
end

function this:destroy(options)
end

function this:test()
  self.photoTex = options.tex
  self.bindComponents.imgPreview.texture = self.photoTex
  local mat = C_UnityObject.Instantiate(self.bindComponents.imgPreview.material)
  mat:EnableKeyword("FIX_TO_GAMMA")
  self.bindComponents.imgPreview.material = mat
  self:initCaptureCanvas()
end

function this:initCaptureCanvas()
  local canvas = L_ResPool:syncGameObject("Program/UIScene/pre_UICaptureCanvas.prefab", C_LuaUtility.GetRootNode())
  self.captureCanvas = canvas
  local cam = canvas.transform:Find("UICaptureCamera"):GetComponent(typeof(CS.UnityEngine.Camera))
  self.cam = cam
end

function this:copyRoot()
  local targetSize = C_Vector2(self.rt.width, self.rt.height)
  local curSize = self.bindComponents.imgRoot.sizeDelta
  local scale = C_Vector3(targetSize.x / curSize.x, targetSize.y / curSize.y, 1)
  local rectMask = self.bindComponents.photoMask
  local padding = rectMask.padding
  if scale.x < 1 or scale.y < 1 then
    rectMask.padding = C_Vector4(rectMask.padding.x * scale.x, rectMask.padding.y * scale.y, rectMask.padding.z * scale.x, rectMask.padding.w * scale.y)
  end
  local newRoot = C_UnityObject.Instantiate(self.bindComponents.imgRoot, self.captureCanvas.transform)
  rectMask.padding = padding
  newRoot.transform.localPosition = C_Vector3.zero
  newRoot.transform.localScale = scale
  return scale
end

function this:save()
  L_PhotoManager:sendEvent(L_PhotoManager.event.photoEditSave)
  self:initCaptureCanvas()
  local newScale = self:copyRoot()
  local camRt = C_RenderTexture.GetTemporary(self.rt.width, self.rt.height)
  self.cam.targetTexture = camRt
  self.cam:Render()
  local rectMask = self.bindComponents.photoMask
  local maskRectTransform = rectMask.gameObject:GetComponent(typeof(C_RectTransform))
  local size = maskRectTransform.rect.size
  local targetSize = C_Vector2(self.rt.width, self.rt.height)
  local leftBottom = C_Vector2(rectMask.padding.x, rectMask.padding.y)
  local maskSize = C_Vector2(size.x - rectMask.padding.x - rectMask.padding.z, size.y - rectMask.padding.y - rectMask.padding.w)
  leftBottom = C_Vector2(leftBottom.x * newScale.x, leftBottom.y * newScale.y)
  maskSize = C_Vector2(maskSize.x * newScale.x, maskSize.y * newScale.y)
  local cutRect = C_Rect(leftBottom, maskSize)
  local texture = C_CameraCaptureUtil.GetTextureFromRT(self.cam.targetTexture, cutRect)
  C_CameraCaptureUtil.SetAlphaOne(texture)
  if self.captureCanvas then
    C_GameObject.Destroy(self.captureCanvas)
  end
  L_UI:close(self.pageName)
  if self.onEditComplete then
    self.onEditComplete(texture)
  end
end

return this
