package com.bc_chat.mine.setting

import com.alibaba.android.arouter.facade.annotation.Route
import com.bc_chat.bc_base.config.RouteConfig
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.bc_base.utils.gone
import com.bc_chat.bc_base.utils.visible
import com.bc_chat.bc_base.view.PictureDialogFragment
import com.bc_chat.mine.R
import com.bc_chat.mine.contract.FeedbackContract
import com.bc_chat.mine.databinding.ActivityFeedbackBinding
import com.bc_chat.mine.presenter.FeedbackPresenter
import com.zhaohaoting.framework.abs.BaseActivity
import com.zhaohaoting.framework.utils.ToastUtils
import com.zhaohaoting.framework.utils.glid.GlideUtils
import java.lang.Exception

/**
 * author：Created by zhaohaoting on 2019/3/19
 * email：526309416@qq.com
 * desc：
 */
@Route(path = RouteConfig.FEEDBACK_ACTIVITY)
class FeedbackActivity : BaseActivity<FeedbackPresenter>(), FeedbackContract.View {

    private val binding by lazy {
        rootDataBinding as ActivityFeedbackBinding
    }

    private var photoPath: String? = null

    override fun initPresenter(): FeedbackPresenter = FeedbackPresenter(this)

    override fun getLayoutResId(): Int = R.layout.activity_feedback

    override fun initialize() {
        binding.selectPicture.setOnClickListener {
            judgePermission(Const.Permission.Key.STORAGE_CAMERA, *Const.Permission.Value.STORAGE_CAMERA)
        }
        binding.ivDelete.setOnClickListener {
            photoPath = ""
            binding.fileContent.visible()
            binding.flAvatar.gone()
        }
        binding.btnCommit.setOnClickListener {
            val etContent = binding.etContent.text.toString()
            val etPhone = binding.etPhone.text.toString()
            if (etContent.isNullOrEmpty()) {
                ToastUtils.show("请输入反馈内容")
                return@setOnClickListener
            }
            if (etPhone.isNullOrEmpty()) {
                ToastUtils.show("请输入电话号码")
                return@setOnClickListener
            }
            if (photoPath.isNullOrEmpty()) {
                presenter.upload(arrayListOf())
            } else {
                presenter.upload(arrayListOf(photoPath))
            }
        }
    }

    override fun requestPermissionSuccess(requestCode: Int) {
        val pictureDialogFragment = PictureDialogFragment()
        pictureDialogFragment.setPicturePhotoPathLinstener {
            photoPath = it
            GlideUtils.loadCenterCropImage(this, "file://$it", binding.ivAvatar)
            binding.fileContent.gone()
            binding.flAvatar.visible()
        }
        pictureDialogFragment.show(fragmentManager)
    }

    override fun uploadSuccess(paths: MutableList<String>?) {
        presenter.feedback(paths, binding.etContent.text.toString(), binding.etPhone.text.toString())
    }

    override fun uploadFailure(e: Exception?) {
        ToastUtils.show("图片上传失败")
    }

    override fun feedbackSuccess() {
        ToastUtils.show("提交成功")
        finish()
    }

    override fun feedbackFailure() {
    }
}