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

import android.content.ClipData
import android.content.ClipDescription
import android.content.Context
import android.graphics.drawable.Drawable
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.*
import android.widget.ImageView
import android.widget.LinearLayout
import com.bomostory.sceneeditmodule.basicdata.Actor
import com.bomostory.sceneeditmodule.basicdata.Layer
import com.bomostory.sceneeditmodule.view.EditActorView
import com.bomostory.sceneeditmodule.view.LayerView

import com.example.tfat.myapplication.R
import com.example.tfat.myapplication.SceneEditActivity
import kotlin.collections.ArrayList

class ActorAdapter(var context : Context, var data: ArrayList<ArrayList<String>>, layerView: LayerView, onActorDragListener: OnActorDragListener) : RecyclerView.Adapter<ActorAdapter.ViewHolder>() {
    private var bitmapData = initActorData(data)
    private var onActorDrop = onActorDragListener

    init {
        layerView.setOnDragListener(dragListener())
    }
    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() : View.OnDragListener {
        return View.OnDragListener {v, event ->
            when (event.action) {
                DragEvent.ACTION_DRAG_STARTED -> {
                    return@OnDragListener true
                }
                DragEvent.ACTION_DROP -> {
                    onActorDrop.onActorDropFinish(event.x.toInt() - 100, event.y.toInt() - 100,
                            data[event.clipData.getItemAt(0).text.toString().toInt()][event.clipData.getItemAt(1).text.toString().toInt()])
                    return@OnDragListener true
                }
            }
            return@OnDragListener true
        }
    }

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

    private fun initActorImageView(data: ArrayList<ArrayList<String>>) : 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.setImageDrawable(Drawable.createFromPath(data[i][j]))
                imageViewList.add(imageView)
            }
            actorImageViewList.add(imageViewList)
            imageViewList = ArrayList()
        }
        return actorImageViewList
    }

    class ViewHolder (view : View) : RecyclerView.ViewHolder(view) {
        fun bind(path: String?, position: Int, data: ArrayList<ImageView>) {
            itemView.findViewById<ImageView>(R.id.actor_image).setImageDrawable(Drawable.createFromPath(path))
            itemView.findViewById<ImageView>(R.id.actor_image).setOnTouchListener(boMoOnTouchListener(position,0))
            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])
                data[i].setOnClickListener(View.OnClickListener {
                    itemView.findViewById<ImageView>(R.id.actor_image).setImageDrawable(data[i].drawable)
                    itemView.findViewById<ImageView>(R.id.actor_image).setOnTouchListener(boMoOnTouchListener(position,i))
                })
                var lParams = data[i].layoutParams
                lParams.height = 100
                lParams.width = 100
                data[i].layoutParams = lParams
            }
        }
        private fun boMoOnTouchListener(position: Int, index: 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)
                        dragData.addItem(ClipData.Item(index.toString()))
                        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
            }
        }
    }

    interface OnActorDragListener{
        fun onActorDropFinish(positionX: Int, positionY: Int, resourcePath: String)
    }
}
