package com.zxhl.main.page.activity

import android.content.Intent
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.Constant
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.model.box.AddressEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.RxBus
import com.zxhl.cms.widget.LoadingDialog
import com.zxhl.main.R
import com.zxhl.main.page.adapter.AddressAdapter
import com.zxhl.main.page.contract.ReceiveAddressContract
import com.zxhl.main.page.presenter.ReceiveAddressPresenter
import kotlinx.android.synthetic.main.activity_layout_receive_address.*

class ReceiveAddressActivity : BaseActivity(), ReceiveAddressContract.View,
    OnRecycleItemClickListener<AddressEntity> {
    private var mPresenter: ReceiveAddressPresenter? = null
    private var mAdapter: AddressAdapter? = null
    private var isDefault: String? = ""
    private var order: String? = ""
    private var id: String? = ""
    private var mLoading: LoadingDialog? = null
    override fun onClick(v: View?) {

    }

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

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

    override fun init() {
        mLoading = LoadingDialog.getLoadingDialog(
            mActivity,
            getString(com.zxhl.cms.R.string.wait_ing),
            false,
            false
        )
        isDefault = intent?.data?.getQueryParameter("isDefault") ?: ""
        order = intent.getStringExtra("order")
        id = intent.getStringExtra("id")
        Log.e("MXL","ID"+id)
        id_img_back?.setOnClickListener {
            finish()
        }
        id_tv_add_address?.setOnClickListener {
            JumpUtils.EditAddressReslutJump("", "", "", "", "", "0")
        }
        mPresenter = ReceiveAddressPresenter(this)
        mPresenter?.getAddress(isDefault)
        mAdapter = AddressAdapter(this, this)
        id_rl_address?.layoutManager = LinearLayoutManager(this)
        id_rl_address?.adapter = mAdapter
    }

    override fun onResume() {
        super.onResume()
        mPresenter?.getAddress(isDefault)
    }

    override fun setAddressData(reslut: List<AddressEntity>?) {
        mAdapter?.clear()
        mAdapter?.appendToList(reslut)
        mAdapter?.notifyDataSetChanged()

    }

    override fun delSucess() {
        if (mLoading != null) {
            mLoading?.dismiss()
        }
        mPresenter?.getAddress(isDefault)
    }

    override fun onItemClick(view: View?, position: Int, data: AddressEntity?) {
        when (view?.id) {
            R.id.id_img_del -> {
                mLoading?.show()
                mLoading?.setLoading("正在删除")
                if(id.equals(data?.id)){
                    Log.e("MXL","删除了刚才的id"+id)
                    RxBus.get().post(Constant.CHOSSEE_ADDRESS_RESULT, 1)
                }
                mPresenter?.delAddress(data)
            }
        }
        if (!TextUtils.isEmpty(order)) {
            when (view?.id) {
                R.id.id_rl_item -> {
                    var intent: Intent = Intent()
                    intent.putExtra("address", data)
                    setResult(1001, intent)
                    finish()
                }
            }

        }
    }

}