package com.dahe.gldriver.ui.waybill.activity

import android.os.Bundle
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.dahe.gldriver.R
import com.dahe.gldriver.adapter.ShowImageAdapter
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.OrderChild
import com.dahe.gldriver.databinding.ActivitySeeDocBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.utils.CommonPopUtils
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.weight.FullyGridLayoutManager
import com.luck.picture.lib.decoration.GridSpacingItemDecoration
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.utils.DensityUtil
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName SeeDocActivity
 * @Author john
 * @Date 2024/3/18 16:03
 * @Description 查看单据
 */
class SeeDocActivity : BaseActivity<ActivitySeeDocBinding>() {
    private lateinit var loadDatas: ShowImageAdapter
    private lateinit var loadBackDatas: ShowImageAdapter
    private lateinit var unLoadDatas: ShowImageAdapter
    private lateinit var unLoadBackDatas: ShowImageAdapter
    private var picList = arrayListOf<String>()
    override fun initView(savedInstanceState: Bundle?) {
        setStatusHeightParams(binding.rlTop)
        setTitleBar("查看单据", true, true)

        binding.run {
            loadDatas = recyclerView.run {
                layoutManager =
                    FullyGridLayoutManager(mContext, 4, GridLayoutManager.VERTICAL, false)
                addItemDecoration(
                    GridSpacingItemDecoration(
                        4,
                        DensityUtil.dip2px(mContext, 8f), false
                    )
                )
                var mData: List<LocalMedia> = ArrayList()
                var myAdapter = ShowImageAdapter()
                adapter = myAdapter
                myAdapter
            }.apply {
                setOnItemClickListener { _, view, position ->
                    CommonPopUtils.getInstance().showPics(
                        mContext,
                        view.findViewById(R.id.fiv),
                        position,
                        recyclerView,
                        items.map { it.path })
                }
            }
            loadBackDatas = recyclerView2.run {
                layoutManager =
                    FullyGridLayoutManager(mContext, 4, GridLayoutManager.VERTICAL, false)
                addItemDecoration(
                    GridSpacingItemDecoration(
                        4,
                        DensityUtil.dip2px(mContext, 8f), false
                    )
                )
                var mData: List<LocalMedia> = ArrayList()
                var myAdapter = ShowImageAdapter()
                adapter = myAdapter
                myAdapter
            }.apply {
                setOnItemClickListener { _, view, position ->
                    CommonPopUtils.getInstance().showPics(
                        mContext,
                        view.findViewById(R.id.fiv),
                        position,
                        recyclerView2,
                        items.map { it.path })
                }
            }

            unLoadDatas = unRecycler.run {
                layoutManager =
                    FullyGridLayoutManager(mContext, 4, GridLayoutManager.VERTICAL, false)
                addItemDecoration(
                    GridSpacingItemDecoration(
                        4,
                        DensityUtil.dip2px(mContext, 8f), false
                    )
                )
                var mData: List<LocalMedia> = ArrayList()
                var myAdapter = ShowImageAdapter()
                adapter = myAdapter
                myAdapter
            }.apply {
                setOnItemClickListener { _, view, position ->
                    CommonPopUtils.getInstance().showPics(
                        mContext,
                        view.findViewById(R.id.fiv),
                        position,
                        unRecycler,
                        items.map { it.path })
                }
            }

            unLoadBackDatas = unRecycler2.run {
                layoutManager =
                    FullyGridLayoutManager(mContext, 4, GridLayoutManager.VERTICAL, false)
                addItemDecoration(
                    GridSpacingItemDecoration(
                        4,
                        DensityUtil.dip2px(mContext, 8f), false
                    )
                )
                var mData: List<LocalMedia> = ArrayList()
                var myAdapter = ShowImageAdapter()
                adapter = myAdapter
                myAdapter
            }.apply {
                setOnItemClickListener { _, view, position ->
                    CommonPopUtils.getInstance().showPics(
                        mContext,
                        view.findViewById(R.id.fiv),
                        position,
                        unRecycler2,
                        items.map { it.path })
                }
            }
        }
    }

    override fun initDate() {
        DataManager.getInstance()
            .orderPhoto(intent.extras?.getString(AppConfig.ORDER_ID).toString())
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<MutableList<OrderChild>>() {
                override fun onSuccess(t: CommonResponseBean<MutableList<OrderChild>>) {
                    super.onSuccess(t)
                    val load = t.data.find { it.type == "1" }
                    val unLoad = t.data.filter { it.type == "2" }.maxBy { it.displayOrder }

                    loadDatas.submitList(t.data.filter { it.type == "1" }
                        .map { LocalMedia().apply { path = it.photoUrl } }.toMutableList())
                    loadBackDatas.submitList(t.data.filter { it.type == "3" && it.receiptType == "4" }
                        .map { LocalMedia().apply { path = it.photoUrl } }.toMutableList())
                    unLoadDatas.submitList(t.data.filter { it.type == "2" }
                        .map { LocalMedia().apply { path = it.photoUrl } }.toMutableList())
                    unLoadBackDatas.submitList(t.data.filter { it.type == "3" && (it.receiptType == "5" || it.receiptType == "6") }
                        .map { LocalMedia().apply { path = it.photoUrl } }.toMutableList())

//                    picList.addAll(t.data.filter { it.type == "1" }.map { it.photoUrl })
//                    picList.addAll(t.data.filter { it.type == "3"&& it.receiptType=="4" }.map { it.photoUrl })
//                    picList.addAll(t.data.filter { it.type == "2" }.map { it.photoUrl })
//                    picList.addAll(t.data.filter { it.type == "3" }.map { it.photoUrl })

                    binding.run {
                        tvLoca.text = load?.location
                        tvUnLoca.text = unLoad?.location
                    }
                }
            }))

    }
}