package com.zxhl.main.page.activity

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.view.animation.AnimationUtils
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.utils.MainLooper
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.LoadingDialog
import com.zxhl.main.R
import com.zxhl.main.page.contract.FeedBackContract
import com.zxhl.main.page.presenter.FeedBackPresenter
import kotlinx.android.synthetic.main.activity_layout_feed_back.*

class FeedBackActivity:BaseActivity(),FeedBackContract.View {
    private var mPresenter: FeedBackPresenter? = null
    private var mLoading: LoadingDialog? = null
    override fun init() {
        mPresenter = FeedBackPresenter(this)
        mLoading = LoadingDialog.getLoadingDialog(
            this,
            getString(com.zxhl.cms.R.string.wait_ing),
            false,
            false
        )
        id_img_back?.setOnClickListener {
            finish()
        }
        id_tv_content.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                if(!TextUtils.isEmpty(p0)){
                    id_tv_length?.text="${p0?.length}/200"
                    id_tv_submit?.setBackgroundResource(R.drawable.shape_aa60ff_r25)
                }else{
                    id_tv_submit?.setBackgroundResource(R.drawable.shape_cccccc_r25)
                }
            }

            override fun afterTextChanged(p0: Editable?) {
                if(!TextUtils.isEmpty(p0)){
                    id_tv_length?.text="${p0?.length}/200"
                    id_tv_submit?.setBackgroundResource(R.drawable.shape_aa60ff_r25)
                }else{
                    id_tv_length?.text="0/200"
                    id_tv_submit?.setBackgroundResource(R.drawable.shape_cccccc_r25)
                }
            }

        })
        id_tv_submit?.setOnClickListener {
            if (TextUtils.isEmpty(id_tv_content?.text.toString())
            ) {
                Utils.showToast(this, "请输入内容哦")
                return@setOnClickListener
            }
            mLoading?.setLoading("请稍后")
            mLoading?.show()
            mPresenter?.feedback(
                id_tv_content?.text.toString()
            )

        }
    }
    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_feed_back
    }

    override fun postSuc(msg: String) {
        mLoading?.setResult(true,"感谢您的反馈,结果将以系统消息形式给您反馈",2000)
        MainLooper.get()
            ?.postDelayed(Runnable {
                finish()
            }, 2000)

    }

    override fun PostFail() {
        if (mLoading != null) {
            mLoading?.dismiss()
        }
        Utils.showToast(this, "提交失败,请稍候再试")
    }
}