package cn.muchinfo.rma.view.base.procurement

import android.content.Intent
import android.graphics.Color
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import androidx.fragment.app.Fragment
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.data.ContractData
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.home.contract.emptyView
import cn.muchinfo.rma.view.base.home.registration.InvoiceRegistrationActivity
import cn.muchinfo.rma.view.base.home.registration.MidpointsRegistrationActivity
import cn.muchinfo.rma.view.base.home.registration.PaymentRegistrationActivity
import cn.muchinfo.rma.view.base.home.registration.SettlementRegistrationActivity
import cn.muchinfo.rma.view.base.procurement.data.ProcurementData
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import mtp.polymer.com.autowidget.adapter.BaseViewHolder
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.coroutines.onScrollChange


class ProcurementPriceViewHolder(
    private val baseFragment: Fragment,
    private val viewModel: ProcurementViewModel,
    private val mHeadHeaderHSView: SyncHScrollView,
    private val type : String
) : BaseViewHolder<ContractData>(baseFragment.context!!) {
    override val itemSize: IntArray = intArrayOf(matchParent, wrapContent)
    lateinit var headTextView1: _RelativeLayout
    lateinit var scrollView1: SyncHScrollView

    override fun _FrameLayout.createContentView() {
        verticalLayout {

            data.bindOptional(context) {
                if (it?.indexSelect == 0) {
                    backgroundResource = R.color.white
                } else if (it?.indexSelect == 1) {
                    backgroundResource = R.color.rma_list_select_color
                }
            }

            relativeLayout {
                isClickable = true
                headTextView1 = this
                setOnTouchListener(ListViewAndHeadViewTouchListener())
                linearLayout {
                    gravity = Gravity.CENTER_VERTICAL
                    onThrottleFirstClick {
                        viewModel.resetDataList(dataIndex, type)
                    }
                    verticalLayout {

                        textView {
                            data.bindOptional(context){
                                text = it?.accountname
                            }
                            textColorInt = R.color.rma_black_33
                            textSizeAuto = 31
                        }.lparams(wrapContent, wrapContent)
                        textView {
                            data.bindOptional(context){
                                text = it?.spotcontractid
                            }
                            textColorStr = "#AAAAAA"
                            textSizeAuto = 24
                        }.lparams(wrapContent, wrapContent)
                    }.lparams(wrapContent, wrapContent) {
                        marginStart = autoSize(32)
                    }

                    view {

                    }.lparams(0, autoSize(1), 1f)

                    imageView {
                        data.bindOptional(context) {
                            if (it?.indexSelect == 0) {
                                imageResource = R.mipmap.rma_down
                            } else if (it?.indexSelect == 1) {
                                imageResource = R.mipmap.rma_up
                            }
                        }
                    }.lparams(autoSize(36), autoSize(36)) {
                        marginEnd = autoSize(32)
                        topMargin = autoSize(5)
                    }
                }.lparams(autoSize(324), autoSize(100))

                interceptScrollContainer {
                    isFocusable = false
//                    setOnTouchListener(ListViewAndHeadViewTouchListener())
                    syncHScrollView {
                        LogUtils.eTag("akshdkjahsdkja","created")
                        scrollView1 = this
                        isFocusable = false
                        scrollBarSize = 0

                        linearLayout {
                            data.bindOptional(context){
                                it?.rightData?.forEach {
                                    headTextView(it)
                                }
                                mHeadHeaderHSView.AddOnScrollChangedListener(

                                    OnScrollChangedListenerImp(scrollView1)
                                )
                                scrollView1.scrollTo(mHeadHeaderHSView.scrollX,mHeadHeaderHSView.scrollY)
                            }
                        }
                    }
                }.lparams(matchParent, autoSize(100)) {
                    marginStart = autoSize(334)
                }
            }.lparams(matchParent, autoSize(100))
            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                data.bindOptional(context) {
                    if (it?.indexSelect == 0) {
                        visibility = View.GONE
                    } else if (it?.indexSelect == 1) {
                        visibility = View.VISIBLE
                    }
                }
                emptyView()
                 textView {
                     onThrottleFirstClick {
                         val intent = Intent()
                         intent.putExtra("data",data.value)
                         intent.putExtra("type","1")
                         intent.setClass(context, SettlementRegistrationActivity::class.java)
                         ActivityUtils.startActivity(intent)
                     }
                    if (type == "3"){
                        visibility = View.VISIBLE
                    }else{
                        visibility = View.GONE
                    }
                    gravity = Gravity.CENTER
                    backgroundResource = R.drawable.rma_item_click_bg
                    text = "结算登记"
                    textSizeAuto = 26
                    textColorInt = R.color.rma_blue_color
                }.lparams(autoSize(168), autoSize(50)) {
                    marginEnd = autoSize(24)
                }
                 textView {
                     onThrottleFirstClick {
                         val intent = Intent()
                         intent.putExtra("data",data.value)
                         intent.putExtra("type","1")
                         intent.setClass(context, PaymentRegistrationActivity::class.java)
                         ActivityUtils.startActivity(intent)
                     }
                    if (type == "3"){
                        visibility = View.VISIBLE
                    }else{
                        visibility = View.GONE
                    }
                    gravity = Gravity.CENTER
                    backgroundResource = R.drawable.rma_item_click_bg
                    text = "款项登记"
                    textSizeAuto = 26
                    textColorInt = R.color.rma_blue_color
                }.lparams(autoSize(168), autoSize(50)) {
                    marginEnd = autoSize(24)
                }
                textView {
                    onThrottleFirstClick {
                        val intent = Intent()
                        intent.putExtra("data",data.value)
                        intent.putExtra("type","1")
                        intent.setClass(context,MidpointsRegistrationActivity::class.java)
                        ActivityUtils.startActivity(intent)
                    }
                    visibility = if (type == "2"){
                        View.VISIBLE
                    }else{
                        View.GONE
                    }
                    gravity = Gravity.CENTER
                    backgroundResource = R.drawable.rma_item_click_bg
                    text = "点价登记"
                    textSizeAuto = 26
                    textColorInt = R.color.rma_blue_color
                }.lparams(autoSize(168), autoSize(50)) {
                    marginEnd = autoSize(24)
                }

                textView {
                    if (type == "2" || type == "1"){
                        visibility = View.VISIBLE
                    }else{
                        visibility = View.GONE
                    }
                    gravity = Gravity.CENTER
                    backgroundResource = R.drawable.rma_item_click_bg
                    text = "详情"
                    textSizeAuto = 26
                    textColorInt = R.color.rma_blue_color
                }.lparams(autoSize(120), autoSize(50)) {
                    marginEnd = autoSize(36)
                }

                imageView {
                    if (type == "3"){
                        visibility = View.VISIBLE
                    }else{
                        visibility = View.GONE
                    }
                    onThrottleFirstClick {
                        ListPopWindow.Builder(attachView = this,backgroundColor = Color.parseColor("#90E5F8FF"),textColor = "#333333",isShowMark = false)
                            .addMenuItem(ListPopWindow.IconParam( title = "发票登记", onItemClickListener = {
                                val intent = Intent()
                                intent.putExtra("data",data.value)
                                intent.putExtra("type","1")
                                intent.setClass(context, InvoiceRegistrationActivity::class.java)
                                ActivityUtils.startActivity(intent)

                            }))
                            .addMenuItem(ListPopWindow.IconParam( title = "入库登记", onItemClickListener = {
                                ToastUtils.showLong("入库登记")
                            }))
                            .addMenuItem(ListPopWindow.IconParam(title = "详情", onItemClickListener = {
                                ToastUtils.showLong("详情")
                            }))
                            .build()
                            .show()
                    }

                    imageResource = R.mipmap.rma_more_one
                }.lparams(autoSize(50), autoSize(50)){
                    marginEnd = autoSize(36)
                }

            }.lparams(matchParent, autoSize(92))
        }.lparams(matchParent, wrapContent)

    }

    var startX = 0
    var startY = 0
    var contentX = 0
    var contentY = 0
    /**
     * TODO 划重点：用来将头部和列表上面的触摸事件都分发给头部的滑动控件，并且计算在左右滑动式告知recyclerview事件消耗
     */
    internal inner class ListViewAndHeadViewTouchListener : View.OnTouchListener {


        override fun onTouch(arg0: View, ev: MotionEvent): Boolean {
            // 当在列头 和 listView控件上touch时，将这个touch的事件分发给 ScrollView
            if (ev.action == MotionEvent.ACTION_DOWN) {
                startX = ev.getX().toInt()
                startY = ev.getY().toInt()
            } else if (ev.action == MotionEvent.ACTION_MOVE) {
                contentX = ev.x.toInt()
                contentY = ev.y.toInt()
            }

            if (contentX != 0 && contentY != 0) {
                if (Math.abs(contentX.minus(startX)) > Math.abs(contentY.minus(startY))) {
                    if (Math.abs(contentX.minus(startX)) > 50) {
                        mHeadHeaderHSView.onTouchEvent(ev)
                        arg0.parent.requestDisallowInterceptTouchEvent(true)
                    }

                } else {
                    if (Math.abs(contentY.minus(startY)) > 50){
                        arg0.parent.requestDisallowInterceptTouchEvent(false)
                    }
                }
            }
            mHeadHeaderHSView.onTouchEvent(ev)
            return false
        }
    }

    open class OnScrollChangedListenerImp(var mScrollViewArg: SyncHScrollView) :
        SyncHScrollView.OnScrollChangedListener {

        override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
            LogUtils.eTag("akshdkjahsdkja","scrollchange")
            mScrollViewArg.smoothScrollTo(l, t)
        }
    }

    fun _LinearLayout.headTextView(content : String) {
        linearLayout {
            gravity = Gravity.CENTER_VERTICAL
            verticalLayout {
                textView {
                    text = content
                    textColorInt = R.color.rma_black_33
                    textSizeAuto = 31
                }.lparams(wrapContent, wrapContent)

            }.lparams(wrapContent, wrapContent)

        }.lparams(autoSize(220), autoSize(100))

    }

}

