package com.qimai.android.widget

import android.graphics.Color
import android.view.*
import android.widget.TextView
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.AttachPopupView
import com.qimai.android.tools.getScreenWidth
import com.qimai.android.widgetlib.popup.AbsQMBaseDialogFragment
import com.qimai.android.widgetlib.popup.QmTipOverlayDrawable
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout

/**
 *   created by wangwei   ON 2019-11-06  email:wangwei_5521@163.com
 *   @version 1.1.1
 *   @Description
 **/
class DemoDialogFragmemt :
    AbsQMBaseDialogFragment() {
    override fun config() = DialogFramgentConfig().apply {
        mWindowWidth = getScreenWidth(context!!)
        mWindowHeight = WindowManager.LayoutParams.MATCH_PARENT
    }

    override fun prepareView(
        inflater: LayoutInflater?,
        container: ViewGroup?
    ): View {
        val view = inflater?.inflate(R.layout.one_flow, container, false)
        val oneAdaptr = object : TagAdapter<String>(listOf("老乡鸡", "香肠", "发Joe", "发Joe房间诶哦")) {
            override fun getView(parent: FlowLayout?, position: Int, t: String?): View {

                val textView = TextView(context)
                textView.setTextColor(Color.parseColor("#FFFFFF"))
                textView.text = t!!
                return textView

            }

            override fun onSelected(position: Int, itemView: View?) {
                super.onSelected(position, view)
//                val root = LayoutInflater.from(context).inflate(R.layout.two_flow, null)
//                val flow = root.findViewById<TagFlowLayout>(R.id.flowLayout)
//                val subAapter = object :
//                    TagAdapter<String>(listOf("老乡鸡", "香肠", "发Joe", "发Joe房间诶哦")) {
//                    override fun getView(
//                        parent: FlowLayout?,
//                        position: Int,
//                        t: String?
//                    ): View {
//
//                        val textView = TextView(context)
//                        textView.text = t!!
//                        return textView
//
//                    }
//
//                    override fun onSelected(position: Int, view: View?) {
//                        super.onSelected(position, view)
//
//                    }
//
//
//                }
//                flow.adapter = subAapter
//                QmToolTip.Builder()
//                    .withContentView(root)
//                    .withArrowWeight(0.5f)
//                    .withAttachedView(itemView)
//                    .withGravity(Gravity.BOTTOM)
//                    .withArrowShow(true)
//                    .build().show()

                XPopup.Builder(context)
                    .atView(view)
                    .hasShadowBg(false)
                    .isCenterHorizontal(true)
                    .asCustom(object : AttachPopupView(context!!) {
                        override fun getImplLayoutId(): Int {
                            return R.layout.one_flow
                        }

                        override fun initPopupContent() {
                            super.initPopupContent()

                            val flow =
                                attachPopupContainer.findViewById<TagFlowLayout>(R.id.flowLayout)

                            attachPopupContainer.background =  QmTipOverlayDrawable(
                                QmTipOverlayDrawable.Builder()
                                    .withGravity(Gravity.TOP)
                                    .withArrowWeight(0.5f)
                                    .withContext(context!!)
                                    .withDefaultOverlayStyle(R.style.QmToolTipOverlayDefault)

                            )


                            val subAapter = object :
                                TagAdapter<String>(listOf("老乡鸡", "香肠", "发Joe", "发Joe房间诶哦")) {
                                override fun getView(
                                    parent: FlowLayout?,
                                    position: Int,
                                    t: String?
                                ): View {

                                    val textView = TextView(context)
                                    textView.text = t!!
                                    return textView

                                }

                                override fun onSelected(position: Int, view: View?) {
                                    super.onSelected(position, view)

                                }


                            }
                            flow.adapter = subAapter

                        }
                    })
                    .show();


            }


        }
        view?.findViewById<TagFlowLayout>(R.id.flowLayout)?.adapter = oneAdaptr

//        val sub = view!!.findViewById<TextView>(R.id.alert_confirm)
//        sub.setOnClickListener{
//            XPopup.Builder(context)
//                .atView(sub)
//                .isCenterHorizontal(true)
//                .hasShadowBg(false)
//                .asCustom(object : AttachPopupView(context!!){
//                    override fun getImplLayoutId(): Int {
//                        return R.layout.tip
//                    }
//                })
//                .show();
//
//
//        }

        return view!!
    }





}