package com.yidianling.consultant.ui.view

import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.PopupWindow
import android.widget.TextView
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.common.tools.RxImageTool
import com.yidianling.consultant.R
import com.yidianling.consultant.listener.OnPriceItemSelectedListener
import com.yidianling.consultant.model.bean.AllFilter
import com.yidianling.consultant.model.bean.Filters
import com.yidianling.consultant.model.bean.PriceRangesItem
import com.yidianling.consultant.model.bean.ReorderItem
import com.yidianling.consultant.ui.view.rangeseekbar.OnRangeChangedListener
import com.yidianling.consultant.ui.view.rangeseekbar.RangeSeekBar
import kotlinx.android.synthetic.main.consultant_ui_price_popup_window.view.*


/**
 * 排序弹窗
 * Created by zqk on 17-9-15.
 */
class PricePopupWindow(
    val context: Context, sortItems: ArrayList<ReorderItem>,
    private val filterData: Filters,
    private val tempFilter: AllFilter,
    var onPriceItemSelectedListener: OnPriceItemSelectedListener
) : PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {

    private val popWidth = (RxDeviceTool.getScreenWidth(context))

    //价格筛选一行三个项
    private val enquirySize = 3
    private val priceRangeViews: ArrayList<View> = ArrayList()
    private var min_Price = ""
    private var max_Price = ""
    private var SeekBarLeftValue = 0f
    private var SeekBarRightValue = 0f
    private val maxPriceValue = 600

    init {
        val view =
            LayoutInflater.from(context).inflate(R.layout.consultant_ui_price_popup_window, null)
        this.contentView = view
        this.isFocusable = true
        @Suppress("DEPRECATION")
        this.setBackgroundDrawable(BitmapDrawable())
        this.isOutsideTouchable = true
        initPriceTag()
        contentView.price_Confirm.setOnClickListener {
//            tempFilter.priceRanges?.min_price=min_Price
//            tempFilter.priceRanges?.max_price=max_Price
            var priceRangesItem = PriceRangesItem(min_Price, max_Price)
            onPriceItemSelectedListener.onPriceItemSelected(priceRangesItem)
            dismiss()
        }
        contentView.range_price_seekbar.setOnRangeChangedListener(object : OnRangeChangedListener {
            override fun onRangeChanged(
                view: RangeSeekBar?,
                leftValue: Float,
                rightValue: Float,
                isFromUser: Boolean
            ) {
                var maxPrice = ((rightValue / 100) * maxPriceValue).toInt().toString()
                if (rightValue > 93) {
                    maxPrice = "无限"
                }
                contentView.tv_start_end_price.text =
                    ((leftValue / 100) * maxPriceValue).toInt().toString() + "-" + maxPrice

            }

            override fun onStartTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) {
            }

            override fun onStopTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) {
            }

        })
    }

    fun initPriceTag() {
        val mWidth = (popWidth - RxImageTool.dp2px(52f)) / enquirySize
        for ((index, priceRangesItem) in filterData.priceRanges.withIndex()) {
            val textView = View.inflate(context, R.layout.consultant_item_filter, null) as TextView
            val params = FrameLayout.LayoutParams(mWidth, RxImageTool.dp2px(48f))
            val marginNum = RxImageTool.dp2px(2f)
            params.setMargins(
                marginNum + (RxImageTool.dp2px(10f) + mWidth) * (index % enquirySize),
                RxImageTool.dp2px(58f) * (index / 3),
                marginNum,
                0
            )
            textView.layoutParams = params
            val contentStr = String.format(
                "%s\n%s",
                priceRangesItem.display_range,
                priceRangesItem.recommend_percent
            )
            val msp = SpannableString(contentStr)
            msp.setSpan(
                AbsoluteSizeSpan(10, true),
                contentStr.indexOf("\n"),
                contentStr.length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            textView.text = msp

            textView.setSingleLine(false)
            textView.setLineSpacing(1.0f, 1.2f)
            priceRangeViews.add(textView)


            if (priceRangesItem == tempFilter.priceRanges) {
                textView.isSelected = true
                textView.background = context.getDrawable(R.drawable.consult_price_expert_selected)
                textView.paint.isFakeBoldText = true
                tempFilter.priceRangesView = textView
            }

            textView.setOnClickListener {
                if (textView != tempFilter.priceRangesView) {
                    tempFilter.priceRangesView?.isSelected = false
                    tempFilter.priceRangesView?.background =
                        context.getDrawable(R.drawable.consult_price_expert_unseleted)
                    tempFilter.priceRangesView?.paint?.isFakeBoldText = false
                }
                if (textView.isSelected) {
                    textView.background =
                        context.getDrawable(R.drawable.consult_price_expert_unseleted)
                    tempFilter.priceRanges = null
                    textView.isSelected = false
                    textView.paint.isFakeBoldText = false
                    tempFilter.priceRangesView = null
                    contentView.tv_start_end_price.text = "0-无限"
                } else {
                    textView.background =
                        context.getDrawable(R.drawable.consult_price_expert_selected)
                    tempFilter.priceRanges = priceRangesItem.copy(
                        min_price = priceRangesItem.min_price,
                        max_price = priceRangesItem.max_price
                    )
                    textView.isSelected = true
                    textView.paint.isFakeBoldText = true
                    tempFilter.priceRangesView = textView
                    if (priceRangesItem.min_price.isNullOrEmpty()) {
                        min_Price = "0"
                        SeekBarLeftValue = 0f
                    } else {
                        min_Price = priceRangesItem.min_price.toString()
                        SeekBarLeftValue =
                            (priceRangesItem.min_price.toString().toFloat() / maxPriceValue) * 100f
                    }
                    if (priceRangesItem.max_price.isNullOrEmpty()) {
                        max_Price = "无限"
                        SeekBarRightValue = 100f
                    } else {
                        max_Price = priceRangesItem.max_price.toString()
                        SeekBarRightValue =
                            (priceRangesItem.max_price.toString().toFloat() / maxPriceValue) * 100f
                    }

                    contentView.range_price_seekbar.setProgress(SeekBarLeftValue, SeekBarRightValue)
                    contentView.tv_start_end_price.text = "$min_Price-$max_Price"
                }

            }
            contentView.flPriceRangeView.addView(textView)
        }
    }


}