package com.renrenliao.im.widget.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.renrenliao.im.R
import com.renrenliao.im.bean.LineEntity
import com.renrenliao.im.bean.LineItem
import com.renrenliao.im.ext.click
import com.renrenliao.im.utils.DataCacheUtils
import com.renrenliao.im.utils.DeviceUtils
import com.renrenliao.im.utils.LogUtils
import com.tencent.mmkv.MMKV
import kotlinx.android.synthetic.main.dialog_router_choose.*


/**
 **@ClassName NickNameDialog
 **@Description
 **@Author gan
 **@Date 2022/5/30 15:15
 **/
@SuppressLint("NotifyDataSetChanged")
class LinesDialog(context: Context, lineEntity: LineEntity)
    : Dialog(context, R.style.dialog_center_full) {
    private val adapter by lazy { LineItemAdapter() }

    init {

        setCanceledOnTouchOutside(true)
        setCancelable(true)
        val view = View.inflate(context, R.layout.dialog_router_choose, null)


        window?.setContentView(view)
        window?.setGravity(Gravity.CENTER)
        window?.setLayout(
            (DeviceUtils.getScreenW(context) - DeviceUtils.dip2px(context,80f)).toInt(),
            WindowManager.LayoutParams.WRAP_CONTENT
            //DeviceUtils.getScreenH(context)-context.statusBarHeight
        )


        mRecyclerView.adapter = adapter
        mRecyclerView.layoutManager = LinearLayoutManager(context)

        val kv = MMKV.defaultMMKV()
        var lastPosition = kv.decodeInt(DataCacheUtils.KEY_LINE_INDEX,0)
        //判断上次设置的位置 在这次中是否越界
        if (lastPosition>=lineEntity.server.size){
            lastPosition = 0
        }
        for (i in lineEntity.server.indices){
            lineEntity.server[i].select = false
            if (i==lastPosition){
                lineEntity.server[i].select = true
            }
        }
        adapter.setList(lineEntity.server)
        //点击item
        adapter.setOnItemClickListener { _, _, position ->
      //      LogUtils.error("点击了 $position")
            for (i in adapter.data.indices){
                adapter.data[i].select = false
                if (i ==position){
                    adapter.data[i].select = true
                }
            }
            adapter.notifyDataSetChanged()
        }
        //点击确定
        btn_sure.click {
            var position =0
            for (i in adapter.data.indices){
                if (adapter.data[i].select){
                    position = i
                }
            }
            DataCacheUtils.setLines(lineEntity,position)
            dismiss()
        }
        //点击关闭
        mImgClose.click {
            dismiss()
        }
    }

    class LineItemAdapter:BaseQuickAdapter<LineItem,BaseViewHolder>(R.layout.item_route_line){
        override fun convert(holder: BaseViewHolder, item: LineItem) {
            val tv_line = holder.getView<TextView>(R.id.tv_line)
            val iv_check = holder.getView<ImageView>(R.id.iv_check)
            tv_line.text = item.name
            iv_check.isSelected = item.select
        }
    }

}