--DailyFortune for Android (DF4A)
--copyright @daisukiKaffuChino
require "mods.func"
require "mods.api"
crypto = require "mods.minicrypto"

isFromHome=...

activity.setContentView(inflate("layout.login"))
initWindowStatusBar()
activity.getSupportActionBar().hide()

glide("ic_launcher_round.webp",logo)

local network=true

isVerifiedCompleted = createObservableBoolean(false, function(oldValue, newValue)
  local saved_username=activity.getSharedData("username")
  if saved_username then
    nameEdt.text=saved_username
    if not isFromHome then
      pwdEdt.text=activity.getSharedData("password","")
      task(100,function()
        action(btn,true)
      end)
    end
  end
end)

local function setClickableTextWithHtml()
  privacyUrl = "https://ys.oftx.top/register"
  agreementUrl = "https://ys.oftx.top/register"

  htmlText = "已阅读并同意" ..
  "<a href=\"" .. agreementUrl .. "\">用户协议</a>" ..
  "和" ..
  "<a href=\"" .. privacyUrl .. "\">隐私政策</a>"

  spannedText = Html.fromHtml(htmlText, Html.FROM_HTML_MODE_LEGACY)

  checkBox.setText(spannedText)
  checkBox.setMovementMethod(LinkMovementMethod.getInstance())
end

setClickableTextWithHtml()

ua=getRandomUserAgent()

btn.onClick=function(v)
  action(v,false)
end

function action(v,crypt)
  local username=nameEdt.text
  local pwd=pwdEdt.text
  local mail=mailEdt.text

  if not network then
    print("没联网")
    return
  end
  if v.text=="注册" then
    if not checkBox.isChecked() then
      print("是不是忘记打钩了")
      return
    end
    if isBlank(username) or isBlank(pwd) or isBlank(mail) then
      print("必填项为空")
      return
    end

    setLoading(true)

    LuaHttp.post({

    })
   else
    if isBlank(username) or isBlank(pwd) then
      print("必填项为空")
      return
    end

    setLoading(true)

    if crypt then
      pwd=crypto.decrypt(pwd,ENCRYPT_KEY)
    end
    LuaHttp.post({

    })
  end
end

mailEdt.addTextChangedListener(TextWatcher{
  afterTextChanged=function(s)
    local email = s.toString()
    if isBlank(email) then
      mailLayout.setError(nil)
     elseif not isValidEmail(email)
      mailLayout.setError("请输入有效的邮箱地址")
     else
      mailLayout.setError(nil)
    end
  end
})

function setLoading(loading)
  nameEdt.enabled=not loading
  pwdEdt.enabled=not loading
  mailEdt.enabled=not loading
  checkBox.enabled=not loading
  btn.enabled=not loading
  if loading then
    progress.visibility=0
    toggle.visibility=8
   else
    progress.visibility=8
    toggle.visibility=0
  end
end


local LuaNetStateChangeObserver=
luajava.bindClass("github.daisukiKaffuChino.observer.LuaNetStateChangeObserver")

LuaNetStateChangeObserver.registerReceiver(this,LuaNetStateChangeObserver.NetStateChangeObserver{
  onConnect=function()
    network=true
  end,
  onDisconnect=function()
    network=false
  end
})

function onDestroy()
  LuaNetStateChangeObserver.unRegisterReceiver(this)
end

function init()
  --不公开的加密逻辑
  isVerifiedCompleted.value=true
end

init()

