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 androidx.recyclerview.widget.LinearLayoutManager
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.adapter.SortRecyclerViewAdapter
import com.yidianling.consultant.constants.ConsultBIConstants
import com.yidianling.consultant.listener.OnPriceItemSelectedListener
import com.yidianling.consultant.listener.OnSortItemSelectedListener
import com.yidianling.consultant.model.bean.AllFilter
import com.yidianling.consultant.model.bean.Filters
import com.yidianling.consultant.model.bean.ReorderItem
import kotlinx.android.synthetic.main.consultant_item_price_range.view.*
import kotlinx.android.synthetic.main.consultant_ui_filter_popup.view.*
import kotlinx.android.synthetic.main.consultant_ui_filter_popup.view.flPriceRange
import kotlinx.android.synthetic.main.consultant_ui_price_popup_window.view.*
import kotlinx.android.synthetic.main.consultant_ui_sort_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 selectedSort: ReorderItem, onPriceItemSelectedListener: OnPriceItemSelectedListener
) : PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {

    private val popWidth = (RxDeviceTool.getScreenWidth(context))
    private val enquirySize = 4
    private val priceRangeViews: ArrayList<View> = ArrayList()
    private var min_Price = ""
    private var max_Price = ""

    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()
    }

    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.paint.isFakeBoldText = true
                tempFilter.priceRangesView = textView
            }

            textView.setOnClickListener {
                if (textView != tempFilter.priceRangesView) {
                    tempFilter.priceRangesView?.isSelected = false
                    tempFilter.priceRangesView?.paint?.isFakeBoldText = false
                }
                if (textView.isSelected) {
                    tempFilter.priceRanges = null
                    textView.isSelected = false
                    textView.paint.isFakeBoldText = false
                    tempFilter.priceRangesView = null
                    contentView.tv_start_end_price.text = "0-无限"
                } else {
                    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"
                    if (priceRangesItem.max_price.isNullOrEmpty()) max_Price = "无限"
                    contentView.tv_start_end_price.text = "$min_Price-$max_Price"
//                    etMinPrice?.setText(priceRangesItem.min_price)
//                    etMaxPrice?.setText(priceRangesItem.max_price)
//                    etMinPrice?.setSelection(etMinPrice?.text!!.length)
//                    etMaxPrice?.setSelection(etMaxPrice?.text!!.length)
                }

//                updateCount(ConsultBIConstants.POSITION_AVERAGE_SERVICE_INPUT)
            }
            contentView.flPriceRangeView.addView(textView)
        }
    }
}