package com.example.tfat.myapplication

import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.RelativeLayout
import org.json.JSONArray
import org.json.JSONObject

class OnTouchBomoSceneListener : View.OnTouchListener{
    lateinit var bomoLayer1 : RelativeLayout
    lateinit var bomo_layer2 : RelativeLayout
    lateinit var bomo_layer3 : RelativeLayout
    lateinit var bomo_layer4 : RelativeLayout
    lateinit var bomo_background : RelativeLayout
    val RECORD_MODE = 1
    val EDIT_MODE = 2
    var isRecord = false
    var positionStart = 0f
    var total_x = 0
    var move_x = 0
    var tempX = 0
    var mx = 0
    var x1 = 0
    var x2 = 0
    var x3 = 0
    var x4 = 0
    var x5 = 0
    var recordNum = 0
    private var recordObject = JSONObject()
    var startRecordTime = 0L
    lateinit var positionRecord : JSONObject
    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
            when (event?.action) {
                MotionEvent.ACTION_DOWN -> {
                    Log.d("TEST12345", "DOWN")
                    positionStart = event.x
                    x1 = bomoLayer1.x.toInt()
                    x2 = bomo_layer2.x.toInt()
                    x3 = bomo_layer3.x.toInt()
                    x4 = bomo_layer4.x.toInt()
                    x5 = bomo_background.x.toInt()
                }
                MotionEvent.ACTION_MOVE -> {
                    //Log.d("TEST12345", "move")
                    mx = (event.rawX - positionStart).toInt()
                    if (total_x < 2000 && total_x > -2000) {
                        bomoLayer1.layout(total_x, bomoLayer1.y.toInt(), total_x + bomoLayer1.width, bomoLayer1.height + bomoLayer1.y.toInt())
                        bomo_layer2.layout(total_x/2, bomo_layer2.y.toInt(), total_x/ 2 + bomo_layer2.width, bomo_layer2.height + bomo_layer2.y.toInt())
                        bomo_layer3.layout(total_x /4, bomo_layer3.y.toInt(), total_x / 4 + bomo_layer3.width, bomo_layer3.height + bomo_layer3.y.toInt())
                        bomo_layer4.layout(total_x/8, bomo_layer4.y.toInt(), total_x / 8 + bomo_layer4.width , bomo_layer4.height + bomo_layer4.y.toInt())
                        bomo_background.layout(total_x/32, bomo_background.y.toInt(), total_x/ 32 + bomo_background.width, bomo_background.height + bomo_background.y.toInt())
                        move_x = mx
                        total_x = mx + tempX
                    } else {
                        move_x = mx
                        if ((total_x <= 2000 && move_x < 0) && (total_x >= -2000) || (total_x >= -2000 && move_x > 0) && (total_x <= 2000)) {
                            total_x = tempX + move_x
                        } else if (total_x > 2000 && move_x < 0) {
                            total_x = tempX + move_x
                        } else if (total_x < -2000 && move_x > 0) {
                            total_x = tempX + move_x
                        }
                        if (total_x > 2000) {
                            total_x = 2000
                        }
                        if (total_x < -2000) {
                            total_x = -2000
                        }
                    }
                    Log.d("TEST12345","x = " + total_x)

                    val obj0 = JSONObject()
                    val jsonObject = JSONObject()
                    val jsonArray = JSONArray()
                    obj0.put("time", System.currentTimeMillis() - startRecordTime)
                    obj0.put("positionX", total_x)
                    jsonArray.put(obj0)
                    jsonObject.put("record", jsonArray)
                    recordObject.put(recordNum.toString(), jsonObject)
                    recordNum ++
                }
                MotionEvent.ACTION_UP -> {
                    Log.d("TEST12345", "up")
                    tempX = total_x
                }
            }
        return true
    }

    fun setLayout(layoutList : ArrayList<LayerView>) {
        bomo_background = layoutList[0]
        bomoLayer1 = layoutList[4]
        bomo_layer2 = layoutList[3]
        bomo_layer3 = layoutList[2]
        bomo_layer4 = layoutList[1]
    }

    fun setMode(isRecord : Boolean){
        this.isRecord = isRecord
    }

    fun getRecord() : JSONObject{
        return recordObject
    }

    fun startRecordTime(time : Long){
        startRecordTime = time
    }

    fun endRecordTime(time : Long){
        val obj0 = JSONObject()
        val jsonObject = JSONObject()
        val jsonArray = JSONArray()
        obj0.put("time", time - startRecordTime)
        obj0.put("positionX", total_x)
        jsonArray.put(obj0)
        jsonObject.put("record", jsonArray)
        recordObject.put(recordNum.toString(), jsonObject)
        recordNum = 0
        recordObject = JSONObject()
    }
}


