package com.example.tfat.myapplication
import android.content.Context
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout

class ActorView : ImageView{
    private var actorData : ActorData

    constructor(context : Context, actorData: ActorData) : super(context) {
        this.setImageBitmap(actorData.bitmap)
        this.actorData = actorData
        var lParams = RelativeLayout.LayoutParams(actorData.size, actorData.size)
        lParams?.setMargins(actorData.x, actorData.y, 0, 0)
        this.layoutParams = lParams
        this.setOnTouchListener(actorOnTouchListener())
    }

    fun actorOnTouchListener() : View.OnTouchListener {
        var positionStart = 0
        var positionEnd = 0
        var mx = 0
        var my = 0
        return View.OnTouchListener { view, motionEvent ->
            when (motionEvent.action) {
                MotionEvent.ACTION_DOWN -> {
                    positionStart = (motionEvent.x).toInt()
                    positionEnd = (motionEvent.y).toInt()
                }
                MotionEvent.ACTION_MOVE -> {
                    mx =  motionEvent.rawX.toInt() - positionStart
                    my =  motionEvent.rawY.toInt() - positionEnd
                    var laParams = view.layoutParams as RelativeLayout.LayoutParams
                    laParams.setMargins(mx,my - 200,0,0)
                    view.layoutParams = laParams
                }
                MotionEvent.ACTION_UP -> {
                    actorData.x = mx
                    actorData.y = my

                }
            }
            return@OnTouchListener true
        }
    }

    fun getData() : ActorData{
        return actorData
    }
}