package com.qimai.android.widget

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.qimai.android.widgetlib.popup.QmToolTip
import com.qimai.android.widgetlib.topbar.QmTopBar
import kotlinx.android.synthetic.main.goodmanager_rightview_titlebar.*

class TipActivity : AppCompatActivity() {

    lateinit var mPop: QmToolTip
    lateinit var topbar: QmTopBar
    var items = mutableListOf("堂食", "外卖")
    private lateinit var  rightText:TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tip)


        topbar = findViewById(R.id.topBar)

        customTopBar()
        findViewById<Button>(R.id.top).setOnClickListener {

            val view = LayoutInflater.from(TipActivity@ this).inflate(R.layout.tip, null)
            QmToolTip.Builder()
                .withAttachedView(findViewById(R.id.top))
                .withContentView(view)
                .withArrowWeight(0.6f)
                .withGravity(Gravity.TOP)
                .build().show()

        }

        findViewById<Button>(R.id.bottom).setOnClickListener {

            val view = LayoutInflater.from(TipActivity@ this).inflate(R.layout.tip, null)
            QmToolTip.Builder()
                .withAttachedView(findViewById<Button>(R.id.bottom))
                .withContentView(view)
                .withArrowShow(true)
                .withGravity(Gravity.BOTTOM)
                .build().show()
  // ceshi
        }
        findViewById<Button>(R.id.topnorrow).setOnClickListener {
            rightText.text = "测试"
            val view = LayoutInflater.from(TipActivity@ this).inflate(R.layout.tip, null)
            QmToolTip.Builder()
                .withAttachedView(findViewById(R.id.topnorrow))
                .withContentView(view)
                .withArrowShow(false)
                .withGravity(Gravity.TOP)
                .build().show()

        }

        findViewById<Button>(R.id.bottomnoArrow).setOnClickListener {

            val view = LayoutInflater.from(TipActivity@ this).inflate(R.layout.tip, null)
            QmToolTip.Builder()
                .withAttachedView(findViewById<Button>(R.id.bottomnoArrow))
                .withContentView(view)
                .withArrowShow(false)
                .withGravity(Gravity.BOTTOM)
                .build().show()

        }


    }

    private class PopListAdapter(
        var datas: MutableList<String>,
        var itemClick: (position: Int) -> Unit
    ) : RecyclerView.Adapter<PopListAdapter.PopListHolder>() {
        private var currentChoosePositon = 0
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PopListHolder {
            return PopListHolder(
                LayoutInflater.from(parent.context).inflate(
                    R.layout.goodmanager_pop_item,
                    parent,
                    false
                )
            )
        }

        override fun getItemCount(): Int = datas.size

        override fun onBindViewHolder(holder: PopListHolder, position: Int) {
            holder.content.text = datas[position]
            holder.container.setOnClickListener {
                currentChoosePositon = position
                itemClick.invoke(position)
            }
            holder.container.isSelected = position == currentChoosePositon

        }


        class PopListHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var content: TextView = itemView.findViewById(R.id.content)
            var container: LinearLayout = itemView.findViewById(R.id.container)
        }
    }

    private fun customTopBar() {
        val topRight =
            layoutInflater.inflate(R.layout.goodmanager_rightview_titlebar, topbar, false)
         rightText = topRight.findViewById<TextView>(R.id.rightText)
        rightText.text = "堂食"
        topbar.customRightView(topRight, null)
        val recyclerView = RecyclerView(this)
        recyclerView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = PopListAdapter(items) {
            mPop.dismiss()
        }

        topRight.setOnClickListener {

            if (!GoodsManagerActivity@ this::mPop.isInitialized) {
                GoodsManagerActivity@ this.mPop = QmToolTip.Builder()
                    .withGravity(Gravity.BOTTOM)
                    .withAttachedView(topRight)
                    .withDefaultOverlayStyle(R.style.tangshi)
                    .withContentView(recyclerView)
                    .withArrowShow(true)
                    .withArrowWeight(0.5f)
                    .withOffset(-100)
                    .build()
            }
            mPop.show()
        }
    }
}
