package com.xyz.dropapp

import android.content.ClipData
import android.content.ClipDescription
import android.os.Bundle
import android.util.Log
import android.view.DragEvent
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.util.regex.Pattern

class MainActivity : AppCompatActivity() {

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

    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)

        textview1.setOnDragListener(onDragListenerTV1)
        textview2.setOnDragListener(onDragListenerTV2)
        textview3.setOnDragListener(onDragListenerTV3)
        
    }

    private val onDragListenerTV1 = View.OnDragListener { view, event ->
        // Defines a variable to store the action type for the incoming event
        val action: Int = event.getAction()
        when (action) {
            DragEvent.ACTION_DRAG_STARTED -> {
                // Determines if this View can accept the dragged data
                val clipDesc = event.clipDescription
                if(clipDesc == null)
                    return@OnDragListener false
                return@OnDragListener if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                    true
                }
                else false
            }
            DragEvent.ACTION_DRAG_ENTERED -> {
                // Applies a GRAY or any color tint to the View. Return true; the return value is ignored.
                //view.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN)
                // Invalidate the view to force a redraw in the new tint
                view.invalidate()
                return@OnDragListener true
            }
            DragEvent.ACTION_DRAG_LOCATION ->         
                return@OnDragListener true
            DragEvent.ACTION_DRAG_EXITED -> {
                //It will clear a color filter .
                //view.getBackground().clearColorFilter()
                // Invalidate the view to force a redraw in the new tint
                view.invalidate()
                return@OnDragListener true
            }
            DragEvent.ACTION_DROP -> {
                // Gets the item containing the dragged data
                val item: ClipData.Item = event.getClipData().getItemAt(0)
                var mType = event.clipDescription.getMimeType(0)
                var dragData: String
                if(mType == "text/plain"){
                    // Gets the text data from the item.
                    dragData = item.text.toString()
                }else{
                    Toast.makeText(applicationContext,"Operation not allowed",Toast.LENGTH_LONG).show()
                    return@OnDragListener true
                }

                // Displays a message containing the dragged data.
                val textview1 = findViewById<TextView>(R.id.textView)
                val tempData = textview1.text.toString()
                textview1.setText(dragData)
                return@OnDragListener true
            }
            DragEvent.ACTION_DRAG_ENDED -> {
                // Turns off any color tinting
                //view.getBackground().clearColorFilter()
                // Invalidates the view to force a redraw
                view.invalidate()

                return@OnDragListener true
            }
            else -> Log.e("Drag App", "Unknown action type received by OnDragListener.")
        }
        return@OnDragListener false
    }

    private val onDragListenerTV2 = View.OnDragListener { view, event ->
        // Defines a variable to store the action type for the incoming event
        val action: Int = event.getAction()
        when (action) {
            DragEvent.ACTION_DRAG_STARTED -> {
                // Determines if this View can accept the dragged data
                val clipDesc = event.clipDescription
                if(clipDesc == null)
                    return@OnDragListener false
                return@OnDragListener if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                    true
                }
                else false
            }
            DragEvent.ACTION_DRAG_ENTERED -> {
                // Applies a GRAY or any color tint to the View. Return true; the return value is ignored.
                //view.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN)
                // Invalidate the view to force a redraw in the new tint
                view.invalidate()
                return@OnDragListener true
            }
            DragEvent.ACTION_DRAG_LOCATION ->                 // Ignore the event
                return@OnDragListener true
            DragEvent.ACTION_DRAG_EXITED -> {
                //It will clear a color filter .
                //view.getBackground().clearColorFilter()
                // Invalidate the view to force a redraw in the new tint
                view.invalidate()
                return@OnDragListener true
            }
            DragEvent.ACTION_DROP -> {
                // Gets the item containing the dragged data
                val item: ClipData.Item = event.getClipData().getItemAt(0)
                var mType = event.clipDescription.getMimeType(0)
                var dragData: String
                if(mType == "text/plain"){
                    // Gets the text data from the item.
                    dragData = item.text.toString()
                    if(checkNumber(dragData)){
                        // Displays a message containing the dragged data.
                        val textview2= findViewById<TextView>(R.id.textView2)
                        val tempData = textview2.text.toString()
                        textview2.setText(dragData)
                        return@OnDragListener true
                    }
                    else{
                        Toast.makeText(applicationContext,"Operation not allowed",Toast.LENGTH_LONG).show()
                        return@OnDragListener true
                    }

                }else{
                    Toast.makeText(applicationContext,"Operation not allowed",Toast.LENGTH_LONG).show()
                    return@OnDragListener true
                }
            }
            DragEvent.ACTION_DRAG_ENDED -> {
                // Turns off any color tinting
                //view.getBackground().clearColorFilter()
                // Invalidates the view to force a redraw
                view.invalidate()

                return@OnDragListener true
            }
            else -> Log.e("Drag App", "Unknown action type received by OnDragListener.")
        }
        return@OnDragListener false
    }

    private val onDragListenerTV3 = View.OnDragListener { view, event ->
        // Defines a variable to store the action type for the incoming event
        val action: Int = event.getAction()
        when (action) {
            DragEvent.ACTION_DRAG_STARTED -> {
                // Determines if this View can accept the dragged data
                val clipDesc = event.clipDescription
                if(clipDesc == null)
                    return@OnDragListener false
                return@OnDragListener if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                    true
                }
                else false
            }
            DragEvent.ACTION_DRAG_ENTERED -> {
                // Applies a GRAY or any color tint to the View. Return true; the return value is ignored.
                //view.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN)
                // Invalidate the view to force a redraw in the new tint
                view.invalidate()
                return@OnDragListener true
            }
            DragEvent.ACTION_DRAG_LOCATION ->                 // Ignore the event
                return@OnDragListener true
            DragEvent.ACTION_DRAG_EXITED -> {
                //It will clear a color filter .
                //view.getBackground().clearColorFilter()
                // Invalidate the view to force a redraw in the new tint
                view.invalidate()
                return@OnDragListener true
            }
            DragEvent.ACTION_DROP -> {
                // Gets the item containing the dragged data
                val item: ClipData.Item = event.getClipData().getItemAt(0)
                var mType = event.clipDescription.getMimeType(0)
                var dragData: String
                if(mType == "text/plain"){
                    // Gets the text data from the item.
                    dragData = item.text.toString()
                    if(checkEmail(dragData)){
                        // Displays a message containing the dragged data.
                        val textview3 = findViewById<TextView>(R.id.textView3)
                        val tempData = textview3.text.toString()
                        textview3.setText(dragData)
                        return@OnDragListener true
                    }
                    else{
                        Toast.makeText(applicationContext,"Operation not allowed",Toast.LENGTH_LONG).show()
                        return@OnDragListener true
                    }
                }else{
                    Toast.makeText(applicationContext,"Operation not allowed",Toast.LENGTH_LONG).show()
                    return@OnDragListener true
                }


            }
            DragEvent.ACTION_DRAG_ENDED -> {
                // Turns off any color tinting
                //view.getBackground().clearColorFilter()
                // Invalidates the view to force a redraw
                view.invalidate()

                return@OnDragListener true
            }
            else -> Log.e("Drag App", "Unknown action type received by OnDragListener.")
        }
        return@OnDragListener false
    }

    val EMAIL_ADDRESS_PATTERN: Pattern = Pattern.compile(
            "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
                    "\\@" +
                    "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
                    "(" +
                    "\\." +
                    "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
                    ")+"
    )

    val NUMBER_PATTERN: Pattern = Pattern.compile(
            "-?[0-9]+(\\\\.[0-9]+)?"
    )

    private fun checkNumber(number: String): Boolean {
        return NUMBER_PATTERN.matcher(number).matches()
    }

    private fun checkEmail(email: String): Boolean {
        return EMAIL_ADDRESS_PATTERN.matcher(email).matches()
    }
}