package com.example.tfat.myapplication.navigationbar.actor

import android.content.ClipData
import android.content.ClipDescription
import android.content.Context
import android.graphics.Bitmap
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.*
import android.widget.ImageView
import android.widget.LinearLayout
import com.example.tfat.myapplication.ActorData
import com.example.tfat.myapplication.ActorView
import com.example.tfat.myapplication.LayerView
import com.example.tfat.myapplication.R
import kotlin.collections.ArrayList

class ActorAdapter(var context : Context, var data: ArrayList<ArrayList<Bitmap>>, var layout: LayerView) : RecyclerView.Adapter<ActorAdapter.ViewHolder>() {
    private var tarLayout : LayerView? = null
    private var bitmapData : ArrayList<Bitmap>
    private lateinit var layerViewList : ArrayList<LayerView>

    init {
        tarLayout = layout
        bitmapData = initActorData(data)
        tarLayout?.setOnDragListener(dragListener(bitmapData))

    }
    override fun getItemCount(): Int {
        return bitmapData.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(bitmapData?.get(position), position, initActorImageView(data)[position])
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v1 = LayoutInflater.from(parent.context).inflate(R.layout.item_actor, parent, false)
        return ViewHolder(v1)
    }

    private fun dragListener(bitmaps : ArrayList<Bitmap>) : View.OnDragListener {
        return View.OnDragListener {v, event ->
            when (event.action) {
                DragEvent.ACTION_DRAG_STARTED -> {
                    return@OnDragListener true
                }
                DragEvent.ACTION_DROP -> {
                    Log.d("TEST12345","QQQQ")
                    var actorData = ActorData(bitmaps[(event.clipData.getItemAt(0).text.toString().toInt())],
                            event.x.toInt() - 100, event.y.toInt() - 100, 200)
                    var actorView = ActorView(context, actorData)
                    var tempTarget = tarLayout
                    if(event.clipData.getItemAt(0).text.toString().toInt() == 4){
                        var r = (Math.random()*5)
                        tarLayout = layerViewList[r.toInt()]
                    }
                    tarLayout?.actorViewList?.add(actorView)
                    tarLayout?.addView(actorView)
                    tarLayout = tempTarget
                    return@OnDragListener true
                }
            }
            return@OnDragListener true
        }
    }

    fun setTargetLayer(layout: LayerView){
        tarLayout = layout
        tarLayout?.setOnDragListener(dragListener(bitmapData))
    }

    fun setLayer(layoutList : ArrayList<LayerView>){
        layerViewList = layoutList
    }

    private fun initActorData(data: ArrayList<ArrayList<Bitmap>>) : ArrayList<Bitmap>{
        var bitmaps = ArrayList<Bitmap>()
        for(i in 0 until data.size){
            bitmaps.add(data[i][0])
        }
        return bitmaps
    }

    private fun initActorImageView(data: ArrayList<ArrayList<Bitmap>>) : ArrayList<ArrayList<ImageView>>{
        var imageViewList = ArrayList<ImageView>()
        var actorImageViewList = ArrayList<ArrayList<ImageView>>()
        for(i in 0 until data.size){
            for(j in 0 until data[i].size){
                var imageView = ImageView(context)
                imageView.setImageBitmap(data[i][j])
                imageViewList.add(imageView)
            }
            actorImageViewList.add(imageViewList)
            imageViewList = ArrayList()
        }
        return actorImageViewList
    }

    class ViewHolder (view : View) : RecyclerView.ViewHolder(view) {
        fun bind(bitmap: Bitmap?, position: Int, data: ArrayList<ImageView>) {
            itemView.findViewById<ImageView>(R.id.actor_image).setImageBitmap(bitmap)
            itemView.findViewById<ImageView>(R.id.actor_image).setOnTouchListener(boMoOnTouchListener(position))
            itemView.findViewById<LinearLayout>(R.id.actor_rotate).removeAllViews()
            for (i in 0 until data.size) {
                itemView.findViewById<LinearLayout>(R.id.actor_rotate).addView(data[i])
                var lParams = data[i].layoutParams
                lParams.height = 100
                lParams.width = 100
                data[i].layoutParams = lParams
            }
        }
        private fun boMoOnTouchListener(position: Int) : View.OnTouchListener {
            return View.OnTouchListener {view, motionEvent ->
                when (motionEvent.action) {
                    MotionEvent.ACTION_DOWN -> {
                        val item = ClipData.Item(position.toString())
                        val mimeTypes = arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN)
                        val dragData = ClipData(null, mimeTypes, item)
                        val shadow = View.DragShadowBuilder(itemView.findViewById<ImageView>(R.id.actor_image))
                        itemView.findViewById<ImageView>(R.id.actor_image).startDrag(dragData, shadow, null, View.DRAG_FLAG_GLOBAL)
                    }
                }
                return@OnTouchListener true
            }
        }
    }
}
