package com.xyz.dragapp

import android.content.ClipData
import android.content.ClipDescription
import android.graphics.Color
import android.graphics.PorterDuff
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.DragEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    private lateinit var textview1: TextView
    private lateinit var textview2: TextView
    private lateinit var textview3: TextView
    private lateinit var imageview: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        textview1 = findViewById<TextView>(R.id.textView)
        textview2 = findViewById<TextView>(R.id.textView2)
        textview3 = findViewById<TextView>(R.id.textView3)
        imageview = findViewById<ImageView>(R.id.imageView)

        textview1.setOnLongClickListener { v: View ->

            // Create a new ClipData.Item from the ImageView object's tag
            val item = ClipData.Item(v.tag as CharSequence)

            // ClipData, and set its MIME type entry to "text/plain"
            val mimeTypes = arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN)
            val data = ClipData(v.tag.toString(), mimeTypes, item)
            // Instantiates the drag shadow builder.
            val dragshadow = View.DragShadowBuilder(v)
            val flags = View.DRAG_FLAG_GLOBAL
            // Starts the drag
            v.startDrag(data // data to be dragged
                    , dragshadow // drag shadow builder
                    , v // local data about the drag and drop operation
                    , flags 
            )
            return@setOnLongClickListener true
        }

        textview2.setOnLongClickListener { v: View ->

            // Create a new ClipData.Item from the ImageView object's tag
            val item = ClipData.Item(v.tag as CharSequence)

            // ClipData, and set its MIME type entry to "text/plain"
            val mimeTypes = arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN)
            val data = ClipData(v.tag.toString(), mimeTypes, item)
            // Instantiates the drag shadow builder.
            val dragshadow = View.DragShadowBuilder(v)
            val flags = View.DRAG_FLAG_GLOBAL
            // Starts the drag
            v.startDrag(data // data to be dragged
                    , dragshadow // drag shadow builder
                    , v // local data about the drag and drop operation
                    , flags
            )
            return@setOnLongClickListener true
        }

        textview3.setOnLongClickListener { v: View ->

            // Create a new ClipData.Item from the ImageView object's tag
            val item = ClipData.Item(v.tag as CharSequence)

            // ClipData, and set its MIME type entry to "text/plain"
            val mimeTypes = arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN)
            val data = ClipData(v.tag.toString(), mimeTypes, item)
            // Instantiates the drag shadow builder.
            val dragshadow = View.DragShadowBuilder(v)
            val flags = View.DRAG_FLAG_GLOBAL
            // Starts the drag
            v.startDrag(data // data to be dragged
                    , dragshadow // drag shadow builder
                    , v // local data about the drag and drop operation
                    , flags
            )
            return@setOnLongClickListener true
        }

    }
}