package com.lukouguoji.aerologic.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.launcher.ARouter
import com.lukouguoji.aerologic.MineActivity
import com.lukouguoji.aerologic.R
import com.lukouguoji.aerologic.ui.viewModel.MineViewModel
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.router.ARouterConstants
import com.lukouguoji.module_base.service.repository.LoginRepository
import com.lukouguoji.module_base.util.Common

class MineFragment : Fragment(), View.OnClickListener {
    private lateinit var mineActivity: MineActivity
    private lateinit var viewModel: MineViewModel

    private lateinit var toolBack: LinearLayout

    private lateinit var oldPwd: EditText
    private lateinit var newPwd: EditText
    private lateinit var confirmPwd: EditText
    private lateinit var updatePwd: TextView


    private fun initView() {
        mineActivity = requireActivity() as MineActivity
        viewModel = ViewModelProvider(this).get(MineViewModel::class.java)

        toolBack = mineActivity.findViewById(R.id.tool_back)
        oldPwd = mineActivity.findViewById(R.id.oldPwd)
        newPwd = mineActivity.findViewById(R.id.newPwd)
        confirmPwd = mineActivity.findViewById(R.id.confirmPwd)
        updatePwd = mineActivity.findViewById(R.id.updatePwd)

        toolBack.setOnClickListener(this)
        updatePwd.setOnClickListener(this)

        confirmPwd.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                // 此处为得到焦点时的处理内容
            } else {
                if (confirmPwd.text.toString() != newPwd.text.toString()) {
                    Common.showToast(mineActivity, "两次输入的新密码不一致，请重新输入！")
                }
            }
        }


    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_mine, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initView()

        //确认移库 返回结果
        viewModel.updatePwdLive.observe(mineActivity) {
            mineActivity.loadingCancel()
            val status = it.getString("status")
            var dataStatus = it.getString("data")
            val msg = it.getString("msg")
            if (Constant.Result.succ == status && Constant.Result.succ == dataStatus) {
                Common.showToast(mineActivity, "密码修改成功！")
                LoginRepository.logout()
                ARouter.getInstance().build(ARouterConstants.ACTIVITY_URL_LOGIN).navigation()
                FlowBus.with<Any>(ConstantEvent.LOGOUT)
                    .tryEmit(true)
            } else {
                if (msg != null && msg != "") {
                    Common.showToast(mineActivity, msg)
                } else {
                    Common.showToast(mineActivity, "密码修改失败！")
                }
            }

        }
    }

    override fun onClick(v: View?) {
        v?.let {
            when (it.id) {
                R.id.tool_back -> {
                    mineActivity.finish()
                }
                R.id.updatePwd -> {
                    if (oldPwd.text.toString() == "") {
                        Common.showToast(mineActivity, "请输入旧密码！")
                        return
                    }
                    if (newPwd.text.toString() == "") {
                        Common.showToast(mineActivity, "请输入新密码！")
                        return
                    }
                    if (confirmPwd.text.toString() == "") {
                        Common.showToast(mineActivity, "请再次输入新密码！")
                        return
                    }
                    if (confirmPwd.text.toString() != newPwd.text.toString()) {
                        Common.showToast(mineActivity, "两次输入的新密码不一致，请重新输入！")
                        return
                    }
                    //修改密码接口
                    viewModel.updatePwd(oldPwd.text.toString(), newPwd.text.toString())
                }
                else -> {
                    Toast.makeText(mineActivity, "未找到对应操作", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}