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

import android.content.ClipData
import android.content.ClipDescription
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.widget.LinearLayoutManager
import android.view.*
import com.bomostory.sceneeditmodule.basicdata.Actor
import com.bomostory.sceneeditmodule.basicdata.Scene
import com.bomostory.sceneeditmodule.layermanagement.LayerAdapter
import com.example.tfat.myapplication.R
import kotlinx.android.synthetic.main.layer_management_fragment.*
import kotlinx.android.synthetic.main.layer_management_fragment.view.*
import kotlin.collections.ArrayList

class LayerManagementDialog : DialogFragment() {

    private var listener: DialogInterface.OnDismissListener? = null
    private var layerChangeListener: OnLayerChange? = null
    private val layer1Data = ArrayList<Actor>()
    private val layer1Listeners = ArrayList<View.OnTouchListener>()
    private val layer1Adapter = LayerAdapter(layer1Data, layer1Listeners)
    private val layer2Data = ArrayList<Actor>()
    private val layer2Listeners = ArrayList<View.OnTouchListener>()
    private val layer2Adapter = LayerAdapter(layer2Data, layer2Listeners)
    private val layer3Data = ArrayList<Actor>()
    private val layer3Listeners = ArrayList<View.OnTouchListener>()
    private val layer3Adapter = LayerAdapter(layer3Data, layer3Listeners)
    private val layer4Data = ArrayList<Actor>()
    private val layer4Listeners = ArrayList<View.OnTouchListener>()
    private val layer4Adapter = LayerAdapter(layer4Data, layer4Listeners)
    private val layer5Data = ArrayList<Actor>()
    private val layer5Listeners = ArrayList<View.OnTouchListener>()
    private val layer5Adapter = LayerAdapter(layer5Data, layer5Listeners)
    private var selectLayer = 0

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.layer_management_fragment, container).apply {
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
            rv_layer_management_layer1.apply {
                adapter = layer1Adapter
                layoutManager = LinearLayoutManager(context).apply {
                    orientation = LinearLayoutManager.HORIZONTAL
                }
            }
            layout_layer_management_layer1.apply {
                setOnDragListener(onDragListener(1))
                setOnClickListener(onClickLayerListener(1))
            }
            rv_layer_management_layer2.apply {
                adapter = layer2Adapter
                layoutManager = LinearLayoutManager(context).apply {
                    orientation = LinearLayoutManager.HORIZONTAL
                }
            }
            layout_layer_management_layer2.apply {
                setOnDragListener(onDragListener(2))
                setOnClickListener(onClickLayerListener(2))
            }
            rv_layer_management_layer3.apply {
                adapter = layer3Adapter
                layoutManager = LinearLayoutManager(context).apply {
                    orientation = LinearLayoutManager.HORIZONTAL
                }
            }
            layout_layer_management_layer3.apply {
                setOnDragListener(onDragListener(3))
                setOnClickListener(onClickLayerListener(3))
            }
            rv_layer_management_layer4.apply {
                adapter = layer4Adapter
                layoutManager = LinearLayoutManager(context).apply {
                    orientation = LinearLayoutManager.HORIZONTAL
                }
            }
            layout_layer_management_layer4.apply {
                setOnDragListener(onDragListener(4))
                setOnClickListener(onClickLayerListener(4))
            }
            rv_layer_management_layer5.apply {
                adapter = layer5Adapter
                layoutManager = LinearLayoutManager(context).apply {
                    orientation = LinearLayoutManager.HORIZONTAL
                }
            }
            layout_layer_management_layer5.apply {
                setOnDragListener(onDragListener(5))
                setOnClickListener(onClickLayerListener(5))
            }
        }
    }

    override fun onDismiss(dialog: DialogInterface?) {
        super.onDismiss(dialog)
        listener?.onDismiss(dialog)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is DialogInterface.OnDismissListener) {
            listener = context
        } else {
            throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
        }
    }

    override fun onDetach() {
        super.onDetach()
        listener = null
    }

    fun setData(scene: Scene){
        this.layer1Data.apply {
            clear()
            addAll(scene.layers[0].actors)
        }
        this.layer2Data.apply {
            clear()
            addAll(scene.layers[1].actors)
        }
        this.layer3Data.apply {
            clear()
            addAll(scene.layers[2].actors)
        }
        this.layer4Data.apply {
            clear()
            addAll(scene.layers[3].actors)
        }
        this.layer5Data.apply {
            clear()
            addAll(scene.layers[4].actors)
        }
        this.layer1Listeners.apply {
            clear()
            for (i in 0 until layer1Data.size) {
                add(onActorTouchListener(1,i))
            }
        }
        this.layer2Listeners.apply {
            clear()
            for (i in 0 until layer2Data.size) {
                add(onActorTouchListener(2,i))
            }
        }
        this.layer3Listeners.apply {
            clear()
            for (i in 0 until layer3Data.size) {
                add(onActorTouchListener(3,i))
            }
        }
        this.layer4Listeners.apply {
            clear()
            for (i in 0 until layer4Data.size) {
                add(onActorTouchListener(4,i))
            }
        }
        this.layer5Listeners.apply {
            clear()
            for (i in 0 until layer5Data.size) {
                add(onActorTouchListener(5,i))
            }
        }
        this.layer1Adapter.notifyDataSetChanged()
        this.layer2Adapter.notifyDataSetChanged()
        this.layer3Adapter.notifyDataSetChanged()
        this.layer4Adapter.notifyDataSetChanged()
        this.layer5Adapter.notifyDataSetChanged()
    }

    fun setListener(onLayerChange: OnLayerChange){
        layerChangeListener = onLayerChange
    }

    private fun onDragListener(layer: Int) : View.OnDragListener {
        return View.OnDragListener {v, event ->
            when (event.action) {
                DragEvent.ACTION_DRAG_STARTED -> {
                    return@OnDragListener true
                }
                DragEvent.ACTION_DROP -> {
                    val actorPositionZ = event.clipData.getItemAt(0).text.toString().toInt()
                    layerChangeListener?.onLayerChange(selectLayer, layer, actorPositionZ)
                    return@OnDragListener true
                }
            }
            return@OnDragListener true
        }
    }

    private fun onClickLayerListener(layer: Int) : View.OnClickListener {
        return View.OnClickListener {
            selectLayer = layer
            layout_layer_management_layer1.setBackgroundColor(Color.parseColor("#ffffff"))
            layout_layer_management_layer2.setBackgroundColor(Color.parseColor("#ffffff"))
            layout_layer_management_layer3.setBackgroundColor(Color.parseColor("#ffffff"))
            layout_layer_management_layer4.setBackgroundColor(Color.parseColor("#ffffff"))
            layout_layer_management_layer5.setBackgroundColor(Color.parseColor("#ffffff"))
            it.setBackgroundColor(Color.parseColor("#efeae1"))
        }
    }

    private fun onActorTouchListener(layer: Int,position: Int) : View.OnTouchListener {
        return View.OnTouchListener {view, motionEvent ->
            when (motionEvent.action) {
                MotionEvent.ACTION_DOWN -> {
                    when (layer == selectLayer) {
                        true -> {
                            val item = ClipData.Item(position.toString())
                            val mimeTypes = arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN)
                            val dragData = ClipData(null, mimeTypes, item)
                            val shadow = View.DragShadowBuilder(view)
                            view.startDrag(dragData, shadow, null, View.DRAG_FLAG_GLOBAL)
                        }
                        false -> {
                            selectLayer = layer
                            layout_layer_management_layer1.setBackgroundColor(Color.parseColor("#ffffff"))
                            layout_layer_management_layer2.setBackgroundColor(Color.parseColor("#ffffff"))
                            layout_layer_management_layer3.setBackgroundColor(Color.parseColor("#ffffff"))
                            layout_layer_management_layer4.setBackgroundColor(Color.parseColor("#ffffff"))
                            layout_layer_management_layer5.setBackgroundColor(Color.parseColor("#ffffff"))
                            when(selectLayer){
                                1 -> layout_layer_management_layer1.setBackgroundColor(Color.parseColor("#efeae1"))
                                2 -> layout_layer_management_layer2.setBackgroundColor(Color.parseColor("#efeae1"))
                                3 -> layout_layer_management_layer3.setBackgroundColor(Color.parseColor("#efeae1"))
                                4 -> layout_layer_management_layer4.setBackgroundColor(Color.parseColor("#efeae1"))
                                5 -> layout_layer_management_layer5.setBackgroundColor(Color.parseColor("#efeae1"))
                            }
                        }
                    }

                }
            }
            return@OnTouchListener true
        }
    }

    interface OnLayerChange{
        fun onLayerChange(layerFrom: Int, layerTo: Int, position: Int)
    }
}