package com.zxhl.main.page.activity

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.model.uc.UserInfo
import com.zxhl.cms.utils.AdCallback
import com.zxhl.cms.utils.NoDoubleClickListener
import com.zxhl.cms.utils.UserDataUtils
import com.zxhl.cms.widget.LoadingDialog
import com.zxhl.main.R
import com.zxhl.main.page.contract.EditUserInfoContract
import com.zxhl.main.page.presenter.EditUserInfoPresenter
import kotlinx.android.synthetic.main.activity_layout_update_introdution.*
import kotlinx.android.synthetic.main.activity_layout_update_introdution.id_img_back
import kotlinx.android.synthetic.main.activity_layout_update_introdution.id_tv_length

class UpdateIntrodutionActivity : BaseActivity(), EditUserInfoContract.View ,AdCallback<String>{
    private var mPresenter: EditUserInfoPresenter? = null
    private var mLoading: LoadingDialog? = null
    override fun init() {
        var desc = intent?.data?.getQueryParameter("description") ?: ""
        id_edit_introdution.setText(desc)
        id_tv_length?.text = id_edit_introdution.length().toString() + "/140"
        mPresenter = EditUserInfoPresenter(this)
        mLoading = LoadingDialog.getLoadingDialog(
            this,
            getString(com.zxhl.cms.R.string.wait_ing),
            false,
            false
        )

        id_img_back?.setOnClickListener {
            finish()
        }
        id_tv_save_introduction?.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                saveIntroduction()
            }

        })
        id_edit_introdution?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (s != null) {
                    id_tv_length?.text = s!!.length.toString() + "/140"
                }

            }

            override fun afterTextChanged(s: Editable?) {

            }

        })
    }

    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }

    override fun onClick(v: View?) {

    }

    override fun layoutID(): Int {
        return R.layout.activity_layout_update_introdution
    }

    fun saveIntroduction() {
        if (!TextUtils.isEmpty(id_edit_introdution.text.toString())) {
            mLoading?.setLoading("loading")
            mLoading?.show()
            mPresenter?.updateIntroduction(id_edit_introdution.text.toString())
        } else {
            // showToast("")
        }
    }

    override fun setData(result: UserInfo?) {

    }

    override fun updateSuc() {
        mLoading?.setResult(true, "success", 1000)
        UserDataUtils.updateUserInfo(this)

    }

    override fun updateFail() {
        mLoading?.setResult(false, "fail", 1000)
    }

    override fun uploadSuc(result: String) {

    }

    override fun uploadFail() {

    }

    override fun updatePicSuc() {

    }

    override fun updatePicFail() {

    }

    override fun onResult(code: Int, result: String?) {
        finish()
    }
}