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.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.ReportContract
import com.zxhl.main.page.presenter.ReportPresenter
import kotlinx.android.synthetic.main.activity_layout_feed_back.*
import kotlinx.android.synthetic.main.activity_layout_report.*
import kotlinx.android.synthetic.main.activity_layout_report.id_img_back
import kotlinx.android.synthetic.main.activity_layout_report.id_tv_content
import kotlinx.android.synthetic.main.activity_layout_report.id_tv_length
import kotlinx.android.synthetic.main.activity_layout_report.id_tv_submit

class ReportActivity : BaseActivity(),ReportContract.View {
    private var mPresenter: ReportPresenter? = null
    private var mLoading: LoadingDialog? = null
    override fun onClick(v: View?) {

    }
    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }
    override fun layoutID(): Int {
        return R.layout.activity_layout_report
    }

    override fun init() {
        var userId = intent?.data?.getQueryParameter("userId") ?: ""
        mPresenter = ReportPresenter(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_length?.text="0/200"
                  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
            }
            mPresenter?.report(
                id_tv_content?.text.toString(),
                userId
            )
            mLoading?.setLoading("请稍后")
            mLoading?.show()
        }
    }

    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, "提交失败,请稍候再试")
    }
}