local this = class("cellSetItem", G_UIModuleBase)
local _keyCodeTpl = L_GameTpl:getKeyCodeTpl()
local _optionDataTpl = L_GameTpl:getOptionDataTpl()
local COUNT_LIMIT = 5

function this.bind()
  return {
    name = "",
    type1 = false,
    type1Text = "",
    type2 = false,
    sliderText = "0",
    sliderValue = 0,
    sliderMin = 0,
    sliderMax = 0,
    closeIcon = false,
    text = true,
    additionFill = false,
    additionBackground = false,
    type3 = false,
    type3Btn1 = false,
    type3Btn2 = false,
    off = false,
    on = false,
    type4 = false,
    type5 = false,
    btnName = "",
    type6 = false,
    type7 = false,
    type7Name = L_WordsTpl:getValue("ui_cellSetItem"),
    type7Icon = false,
    type7Sprite = "",
    type7Text = true,
    type7Mask = false,
    type8 = false,
    type8Text = "",
    type8TextCanvas = 51,
    dropdownCanvas = 50,
    noneCanvas = 50,
    grid_dropCanvas = 30000,
    dropdownToggle = false,
    dropdown = false,
    type9 = false,
    cellChoiceItemList = {
      moduleName = "pages/Option/cellListItem"
    },
    cellChoiceWhiteItem = {
      moduleName = "pages/Option/cellChoiceWhiteItem"
    },
    txt_block = "",
    active_txtBlock = false
  }
end

function this.methods()
  return {
    cellChoiceWhiteItem = {
      onClick = function(self, index, num)
        local isIn = self.bind.cellChoiceWhiteItem:getValue(num, "bgB")
        if isIn then
        else
          for i = 1, #self.bind.cellChoiceWhiteItem do
            local val = {
              bgB = i == num
            }
            self.bind.cellChoiceWhiteItem:change(i, val)
          end
          self:emit("onChange", self.bind.id, index)
        end
      end
    },
    cellChoiceItemList = {
      onClick = function(self, text, index, num)
        if self.bind.id == L_Const.settingType.language and not L_SettingStore:getIsExistsLangPack(index) then
          local callBack = function(text)
            self.bind.type8Text = text
          end
          self:emit("onChange", self.bind.id, index, text, callBack)
        else
          self.bind.type8Text = text
          self:emit("onChange", self.bind.id, index)
        end
        self.bind.dropdownToggle = false
        self.bindComponents.noneCanvas.overrideSorting = false
        self.bindComponents.type8TextCanvas.overrideSorting = false
      end
    },
    onClick_type2ChangeSlider = function(self, value)
      if self.refreshDirty then
        self.refreshDirty = false
      else
        local val = string.format("%d", value)
        self:emit("onChange", self.bind.id, val)
      end
      self:refresh_addition(value)
    end,
    onClick_type3Btn1 = function(self)
      if self:showBlockTips() then
        return
      end
      self.bind.type3Btn1 = false
      self.bind.type3Btn2 = true
      self.bind.off = false
      self.bind.on = true
      self:emit("onChange", self.bind.id, true)
    end,
    onClick_type3Btn2 = function(self)
      self.bind.type3Btn1 = true
      self.bind.type3Btn2 = false
      self.bind.off = true
      self.bind.on = false
      self:emit("onChange", self.bind.id, false)
    end,
    onClick_type4Btn = function(self)
      if self.bind.id == L_Const.settingType.CDK then
        local msg = {command = "CDKEY"}
        L_Net:sendMessage(MsgGenCode.CSProtoGMCommand, msg, nil)
      end
    end,
    onClick_type5Btn = function(self)
      self:emit("onClick", self.bind.id)
    end,
    onClick_type7Btn = function(self)
      local tpl = _optionDataTpl:getTplById(self.bind.id)
      local allowChange = _optionDataTpl:getAllowChange(tpl)
      if allowChange then
        self:emit("changeKey", self.bind.id)
        self.bind.type7Mask = true
        self.bind.type7Text = false
      else
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_keycode_not_support_change"))
      end
    end,
    onClick_type8Btn = function(self)
      self:refresh_dropdown()
      self:emit("changeDropdown", self.bind.id, function()
        self:refresh_dropdown()
      end)
    end
  }
end

function this:toBind(...)
  self.refreshDirty = true
  this.super.toBind(self, ...)
end

function this:refresh()
  self:refreshShowState()
  local tpl = _optionDataTpl:getTplById(self.bind.id)
  self.bind.name = _optionDataTpl:getOptionName(tpl)
  self.bind.cellChoiceWhiteItem:clear()
  if self.bind.optionType == L_Const.optionType.text then
    self:refresh_text()
  elseif self.bind.optionType == L_Const.optionType.slider then
    self:refresh_slider()
  elseif self.bind.optionType == L_Const.optionType.toggle then
    self:refresh_toggle()
  elseif self.bind.optionType == L_Const.optionType.textInput then
    self:refresh_textInput()
  elseif self.bind.optionType == L_Const.optionType.button then
    self:refresh_button()
  elseif self.bind.optionType == L_Const.optionType.multiMenu then
    self:refresh_multiMenu()
  elseif self.bind.optionType == L_Const.optionType.changeKey then
    self:refresh_changeKey()
  elseif self.bind.optionType == L_Const.optionType.dropMenu then
    self:refresh_dropMenu()
  elseif self.bind.optionType == L_Const.optionType.imageSet then
    self:refreshBright(L_SettingStore:getSetData(L_Const.settingType.bright))
    self:refreshContrast(L_SettingStore:getSetData(L_Const.settingType.contrast))
    self:refreshSaturation(L_SettingStore:getSetData(L_Const.settingType.saturation))
  end
  local size = self.gameObject.transform.sizeDelta
  if self.bind.optionType == L_Const.optionType.imageSet then
    self.bind.type9 = true
    size.y = 280
    self.bindComponents.imgPreview1.material = C_UnityObject.Instantiate(self.bindComponents.imgPreview1.material)
    self.bindComponents.imgPreview2.material = C_UnityObject.Instantiate(self.bindComponents.imgPreview2.material)
    self.bindComponents.imgPreview3.material = C_UnityObject.Instantiate(self.bindComponents.imgPreview3.material)
  else
    self.bind.type9 = false
    local size = self.gameObject.transform.sizeDelta
    size.y = 81
  end
  self.gameObject.transform.sizeDelta = size
end

function this:open()
  L_SettingStore:listenCallFunc(L_SettingStore.event.showStateChange, self.onShowStateChange, self)
end

function this:close()
  L_SettingStore:unListenCallFunc(L_SettingStore.event.showStateChange, self.onShowStateChange, self)
end

function this:onShowStateChange(id, settingState, blockType)
  if id == self.bind.id then
    self.bind.settingState = settingState
    if blockType then
      self.bind.blockType = blockType
    end
    self:refreshShowState()
    self.parent:forceRebuildLayout()
  end
end

function this:refreshShowState()
  local canvasGroup = self.bindComponents.canvasGroup
  local settingState = self.bind.settingState
  self:setBlock(false)
  if settingState == L_Const.settingState.none then
    self.gameObject:SetActive(true)
  elseif settingState == L_Const.settingState.block then
    self:setBlock(true, self.bind.blockType)
  elseif settingState == L_Const.settingState.hide then
    self.gameObject:SetActive(false)
  end
  self.parent:refreshDisplayState()
end

function this:refreshBright(bright)
  local value = tonumber(bright)
  if value <= 50 then
    value = 0.012 * value - 0.6
  else
    value = 0.004 * value - 0.2
  end
  self.bindComponents.imgPreview1.material:SetFloat("_Brightness", value)
  self.bindComponents.imgPreview2.material:SetFloat("_Brightness", value)
  self.bindComponents.imgPreview3.material:SetFloat("_Brightness", value)
end

function this:refreshSaturation(saturation)
  local value = tonumber(saturation)
  if value <= 50 then
    value = 0.012 * value + 0.4
  else
    value = 0.008 * value + 0.6
  end
  self.bindComponents.imgPreview1.material:SetFloat("_Saturation", value)
  self.bindComponents.imgPreview2.material:SetFloat("_Saturation", value)
  self.bindComponents.imgPreview3.material:SetFloat("_Saturation", value)
end

function this:refreshContrast(contrast)
  local value = tonumber(contrast)
  value = 0.004 * value + 0.8
  self.bindComponents.imgPreview1.material:SetFloat("_Contrast", value)
  self.bindComponents.imgPreview2.material:SetFloat("_Contrast", value)
  self.bindComponents.imgPreview3.material:SetFloat("_Contrast", value)
end

function this:refresh_text()
  self.bind.type1 = true
  self.bind.type1Text = L_Const.settingText:ReadParam(self.bind.id, tonumber(self.bind.default))
end

function this:refresh_slider()
  local tpl = _optionDataTpl:getTplById(self.bind.id)
  local data = _optionDataTpl:getOptionPara2(tpl)
  self.bind.type2 = true
  self.bind.sliderMin = data[1]
  self.bind.sliderMax = data[2]
  self.bind.sliderText = self.bind.default
  self.bind.sliderValue = tonumber(self.bind.sliderText)
  self.bind.closeIcon = false
  self:refresh_addition(self.bind.sliderValue)
  local slider = self.bindComponents.slider
  if self.bind.id == L_Const.settingType.bgm then
    slider.eventDownName = "Play_SFX_System_UI_Setting_Slide_BGM_On"
    slider.eventUpName = "Play_SFX_System_UI_Setting_Slide_BGM_Off"
  elseif self.bind.id == L_Const.settingType.cv then
    slider.eventDownName = "Play_SFX_System_UI_Setting_Slide_VO_On"
    slider.eventUpName = "Play_SFX_System_UI_Setting_Slide_VO_Off"
  elseif self.bind.id == L_Const.settingType.se then
    slider.eventDownName = "Play_SFX_System_UI_Setting_Slide_SFX_On"
    slider.eventUpName = "Play_SFX_System_UI_Setting_Slide_SFX_Off"
  else
    slider.eventDownName = nil
    slider.eventUpName = nil
  end
  if L_Const.settingType.bgm <= self.bind.id and self.bind.id <= L_Const.settingType.cv and self.bind.sliderText == "0" then
    self.bind.closeIcon = true
  end
  if L_Const.settingType.bgm <= self.bind.id and self.bind.id <= L_Const.settingType.cv and L_SettingStore:getSetData(L_Const.settingType.mute) == "true" then
    self.bind.closeIcon = true
  end
  self.bind.text = not self.bind.closeIcon
end

function this:refresh_toggle()
  self.bind.type3 = true
  if self.bind.default == 0 then
  end
  if self.bind.default == "false" or self.bind.default == "0" then
    self.bind.type3Btn1 = true
    self.bind.type3Btn2 = false
    self.bind.off = true
    self.bind.on = false
  else
    self.bind.type3Btn1 = false
    self.bind.type3Btn2 = true
    self.bind.off = false
    self.bind.on = true
  end
end

function this:refresh_textInput()
  self.bind.type4 = true
end

function this:refresh_button()
  self.bind.type5 = true
  local tpl = _optionDataTpl:getTplById(self.bind.id)
  self.bind.btnName = _optionDataTpl:getOptionPara4(tpl)
end

function this:refresh_multiMenu()
  local tpl = _optionDataTpl:getTplById(self.bind.id)
  local data = _optionDataTpl:getOptionPara6(tpl)
  local defaultId = tonumber(self.bind.default)
  local whiteData = {}
  if self.bind.id == L_Const.settingType.image then
    for i = 1, #data do
      if i == defaultId then
        table.insert(whiteData, {
          text = data[i],
          textB = data[i],
          bgB = true,
          index = i,
          num = i
        })
      else
        table.insert(whiteData, {
          text = data[i],
          textB = data[i],
          bgB = false,
          index = i,
          num = i
        })
      end
    end
  else
    for i = 1, #data do
      if i == defaultId then
        table.insert(whiteData, {
          text = data[i],
          textB = data[i],
          bgB = true,
          index = i,
          num = i
        })
      else
        table.insert(whiteData, {
          text = data[i],
          textB = data[i],
          bgB = false,
          index = i,
          num = i
        })
      end
    end
  end
  self.bind.cellChoiceWhiteItem:insert_array(whiteData)
  self.bind.cellChoiceWhiteItem:change(#data, {line = false})
  self.bind.type6 = true
end

local _sprite_LMB = "UI/Atlas/Guide/tex_guide_fight_icon_sb1.png"
local _sprite_RMB = "UI/Atlas/Guide/tex_guide_fight_icon_sb1.png"
local _sprite_MMB = "UI/Atlas/Guide/tex_guide_fight_icon_sb2.png"

function this:refresh_changeKey()
  self.bind.type7 = true
  self.bind.type7Text = true
  local name
  local tpl = _keyCodeTpl:getTplByKey(self.bind.default)
  if tpl then
    name = _keyCodeTpl:getKeyName(tpl)
    local keyCode = _keyCodeTpl:getKeyCode(tpl)
    if keyCode == "LMB" then
      self.bind.type7Icon = true
      self.bind.type7Sprite = _sprite_LMB
      self.bindComponents.layout.padding.left = 10
      self.bind.type7Name = name
      return
    end
    if keyCode == "RMB" then
      self.bind.type7Icon = true
      self.bind.type7Sprite = _sprite_RMB
      self.bindComponents.mouseIcon.flipHorizontal = true
      self.bindComponents.layout.padding.left = 10
      self.bind.type7Name = name
      return
    end
    if keyCode == "MMB" then
      self.bind.type7Icon = true
      self.bind.type7Sprite = _sprite_MMB
      self.bindComponents.layout.padding.left = 10
      self.bind.type7Name = name
      return
    end
  else
    name = self.bind.default
  end
  self.bind.type7Name = name
  self.bind.type7Icon = false
  self.bindComponents.mouseIcon.flipHorizontal = false
  self.bindComponents.layout.padding.left = 21
end

function this:refresh_dropMenu()
  self.bind.type8 = true
  self.bind.dropdownCanvas = self.bind.defaultCanvas
  self.bind.noneCanvas = self.bind.defaultCanvas
  self.bind.type8TextCanvas = self.bind.defaultCanvas + 1
  self.bind.grid_dropCanvas = self.bind.defaultCanvas + 1
  local tpl = _optionDataTpl:getTplById(self.bind.id)
  local data = _optionDataTpl:getOptionPara6(tpl)
  local defaultId = tonumber(self.bind.default)
  local tempData = {}
  local srcId = 1
  if self.bind.id == L_Const.settingType.resolution then
    srcId = L_SettingStore:getDeviceCapResultionId()
    defaultId = math.max(srcId, defaultId)
  end
  self.bind.dropdown = true
  self.bind.type8Text = data[defaultId]
  print("-------(00000000)---------------->1111111111111")
  if self.bind.id == L_Const.settingType.language then
    print("-------(00000000)---------------->0000000000")
    self.bind.cellChoiceItemList:clear()
    for i = srcId, #data - 1 do
      local size = L_SettingStore:byteToGBText(L_SettingStore:getLangPackSize(i))
      local text = L_SettingStore:getIsExistsLangPack(i) and data[i] or data[i] .. " " .. size
      table.insert(tempData, {
        textW = data[i],
        textG = text,
        index = i
      })
    end
    local size = L_SettingStore:byteToGBText(L_SettingStore:getLangPackSize(#data))
    local textEnd = L_SettingStore:getIsExistsLangPack(#data) and data[#data] or data[#data] .. " " .. size
    table.insert(tempData, {
      textW = data[#data],
      textG = textEnd,
      index = #data,
      line = false
    })
  else
    self.bind.cellChoiceItemList:clear()
    for i = srcId, #data - 1 do
      table.insert(tempData, {
        textW = data[i],
        textG = data[i],
        index = i
      })
    end
    table.insert(tempData, {
      textW = data[#data],
      textG = data[#data],
      index = #data,
      line = false
    })
  end
  self.bind.cellChoiceItemList:insert_array(tempData)
  self.bindComponents.noneCanvas.overrideSorting = false
  self.bindComponents.type8TextCanvas.overrideSorting = false
end

function this:refresh_dropdown()
  if not self.bind.dropdownToggle then
    for i = 1, #self.bind.cellChoiceItemList do
      if self.bind.cellChoiceItemList:getValue(i, "textW") == self.bind.type8Text then
        self.bind.cellChoiceItemList[i].wObj = true
        self.bind.cellChoiceItemList[i].gObj = false
      else
        self.bind.cellChoiceItemList[i].wObj = false
        self.bind.cellChoiceItemList[i].gObj = true
      end
    end
    self.bind.dropdownToggle = true
    self.bindComponents.noneCanvas.overrideSorting = true
    self.bindComponents.type8TextCanvas.overrideSorting = true
  else
    self.bind.dropdownToggle = false
    self.bindComponents.noneCanvas.overrideSorting = false
    self.bindComponents.type8TextCanvas.overrideSorting = false
  end
end

function this:refresh_addition(value)
  if value == self.bind.sliderMin then
    self.bind.additionFill = false
    self.bind.additionBackground = true
    return
  end
  if value == self.bind.sliderMax then
    self.bind.additionFill = true
    self.bind.additionBackground = false
  end
  local curPercent = value / self.bind.sliderMax
  local limitMinPercent = 0.03
  if self.bind.sliderMin > 0 then
    limitMinPercent = self.bind.sliderMin / self.bind.sliderMax
  end
  local limitMaxPercent = 0.97
  if curPercent <= limitMinPercent then
    self.bind.additionFill = false
  elseif curPercent > limitMinPercent and curPercent < limitMaxPercent then
    self.bind.additionFill = true
    self.bind.additionBackground = true
  elseif curPercent >= limitMaxPercent then
    self.bind.additionBackground = false
  end
end

function this:setBlock(isOn, blockType)
  if isOn then
    if blockType then
      self.bind.active_txtBlock = true
      local txt = L_WordsTpl:getValue(L_Const.blockInfo[blockType] or "block")
      self.bind.txt_block = txt
    else
      local canvasGroup = self.bindComponents.canvasGroup
      canvasGroup.alpha = 0.5
      canvasGroup.interactable = false
    end
  else
    self.bind.active_txtBlock = false
    local canvasGroup = self.bindComponents.canvasGroup
    canvasGroup.alpha = 1
    canvasGroup.interactable = true
  end
end

function this:showBlockTips()
  if self.bind.settingState == L_Const.settingState.block then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue(L_Const.blockInfo[self.bind.blockType]))
    return true
  end
  return false
end

return this
