package com.zj365.health.act

import android.Manifest
import android.content.Intent
import android.graphics.Rect
import android.net.Uri
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.androidkun.xtablayout.XTabLayout
import com.xty.base.act.BaseListAct
import com.xty.base.utils.PayUtils
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.ReChangeEvent
import com.xty.network.model.ShopOrderListBean
import com.zj365.health.R
import com.zj365.health.adapter.GoodsOrderAdapter
import com.zj365.health.databinding.ActShopOrderListBinding
import com.zj365.health.vm.ShopOrderVm
import com.zj365.health.weight.DialogTipNew
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import pub.devrel.easypermissions.EasyPermissions

@Route(path = ARouterUrl.SHOP_ORDER_LIST_ACT)
class ShopOrderListAct : BaseListAct<ShopOrderVm>() {

    val binding by lazy { ActShopOrderListBinding.inflate(layoutInflater) }

    var orderType: Int = 0

    val mAdapter by lazy { GoodsOrderAdapter() }

    private val mTitles = mutableListOf<String>()

    var bean :  ShopOrderListBean.ShopOrderBean? = null


    val dialogCancel by lazy {
        DialogTipNew(this, "取消订单","", "确认","取消",false,{
            bean?.let {
                mViewModel.cancelOrder(it.id)

            }
        }){

        }

    }

    val dialogReturn by lazy {
        DialogTipNew(this, "退款申请","", "确认","取消",false,{
            bean?.let {
                mViewModel.applyReturn(it.id)

            }
        }){

        }

    }


    override fun initAdapter() {
        binding.mRecycle.adapter = mAdapter
        binding.mRecycle.layoutManager = LinearLayoutManager(this)
        binding.mRecycle.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, itemPosition: Int, parent: RecyclerView) {
                super.getItemOffsets(outRect, itemPosition, parent)
                outRect.bottom = 12
                outRect.right = 14
                outRect.left = 14
            }
        })

        setRecycleRefresh(binding.mRecycle,binding.mRefresh,true)

        mAdapter.addChildClickViewIds(R.id.tv_confirm_receive_goods,R.id.tv_logistics,R.id.tv_refund)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            var bean = adapter.data[position] as ShopOrderListBean.ShopOrderBean
            bundle.clear()
            bundle.putLong("id",bean.id)
            RouteManager.goAct(ARouterUrl.SHOP_ORDER_DETAIL_ACT,bundle)

        }
        
        mAdapter.setOnItemChildClickListener { adapter, view, position ->

             bean = adapter.data[position] as ShopOrderListBean.ShopOrderBean
            bean?.let {
                when(view.id){
                    R.id.tv_confirm_receive_goods ->{
                        mViewModel.receiveGoods(it.id)
                    }
                    R.id.tv_logistics->{
                        if (it.status == 1){
                            dialogCancel.setContentNew("您是否确认取消订单，取消订单后如涉及退款" +
                                    "，请注意查收")
                            dialogCancel.show()

                        }else if(it.status == 3){

                        }else{
                            if(EasyPermissions.hasPermissions(this,
                                    Manifest.permission.CALL_PHONE)){
                                startActivity(Intent(Intent.ACTION_CALL, Uri.parse("tel:4000063365")))
                            }else{
                                EasyPermissions.requestPermissions(this,"",
                                    0x00011,
                                    Manifest.permission.CALL_PHONE)
                            }
                        }
                    }
                    R.id.tv_refund ->{
                        if (it.status == 3 || it.status == 2){
                            dialogReturn.setContentNew("您是否确认对此订单进行退款申请，申请完成" +
                                    "后会在1~3个工作日对订单进行审核，请及时" +
                                    "关注订单状态")
                            dialogReturn.show()
                        }else if (it.status == 1){
                            PayUtils(this).payForGoods(it.pay_price.toFloat(),it.order_no)

                        }
                    }
                }
            }

        }

    }

    override fun loadData() {
        mViewModel.getOrderList(page,orderType)
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "我的订单"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        initTab()
    }

    fun initTab() {

        mTitles.add("全部订单")
        mTitles.add("待发货")
        mTitles.add("待收货")
        mTitles.add("已完成")
        mTitles.add("已取消")


        //设置tab
        mTitles.forEach { tabData ->
            val tab = binding.xTablayout.newTab()
            tab.text = tabData
            binding.xTablayout.addTab(tab)
        }
        binding.xTablayout.setOnTabSelectedListener(object : XTabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: XTabLayout.Tab?) {
                when(tab!!.position){
                    0->{
                        orderType = 0
                    }
                    1->{
                        orderType = 2
                    }
                    2->{
                        orderType = 3

                    }
                    3->{
                        orderType = 4

                    }
                    4->{
                        orderType = 5

                    }
                }
                page = 1
                mViewModel.getOrderList(page,orderType)

            }

            override fun onTabUnselected(tab: XTabLayout.Tab?) {
            }

            override fun onTabReselected(tab: XTabLayout.Tab?) {
            }
        })
    }

    override fun liveObserver() {
        mViewModel.shopOrderLiveDate.observe(this){
            setDate(mAdapter,it.data.list)
        }

        mViewModel.cancelOrderLiveData.observe(this){
            page = 1
            mViewModel.getOrderList(page,orderType)
        }

        mViewModel.receiveGoodsLiveData.observe(this){
            page = 1
            mViewModel.getOrderList(page,orderType)
        }
    }

    override fun setLayout() = binding.root

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun paySuccess(event: ReChangeEvent){
        page = 1
        loadData()
    }
}