package com.example.tfat.myapplication

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import org.json.JSONObject

class SceneView : FrameLayout{
    private var layerViewList = ArrayList<LayerView>()
    private var backgroundTheme = 0
    private var isRecorded = false
    private var record = JSONObject()
    private var playIcon : ImageView
    private var playView : View
    var startRecordTime = 0L
    var endRecordTime = 0L
    var onTouchBoMoSceneListener : OnTouchBomoSceneListener

    constructor(context:Context, layerViewList : ArrayList<LayerView>, resource : Int, isRecord: Boolean) : super(context) {
        val inflater = LayoutInflater.from(context)
        playView = inflater.inflate(R.layout.scene_view, null)
        playIcon = playView.findViewById(R.id.play)
        this.addView(playView)
        for(layerView in layerViewList){
            if (layerView == layerViewList.first()) {
                layerView.setBackgroundResource(resource)
            }
            this.addView(layerView)
        }
        onTouchBoMoSceneListener = OnTouchBomoSceneListener()
        onTouchBoMoSceneListener.setLayout(layerViewList)
        this.backgroundTheme = resource
        this.layerViewList = layerViewList
        this.isRecorded = isRecord
        this.setOnTouchListener(onTouchBoMoSceneListener)
        if (isRecord) {
            playView.bringToFront()
        }
    }

    fun getLayerView() : ArrayList<LayerView>{
        return layerViewList
    }

    fun setMode(isRecord : Boolean) {
        onTouchBoMoSceneListener.setMode(isRecord)
    }

    fun getAllActor() : ArrayList<ActorView> {
        var actorViewList = ArrayList<ActorView>()
        for(layer in layerViewList) {
            for(actorView in layer.actorViewList){
                actorViewList.add(actorView)
            }
        }
        return actorViewList
    }

    fun getActor(layerNum : Int) : ArrayList<ActorView> {
        var actorViewList = ArrayList<ActorView>()
        for(actorView in layerViewList[layerNum].actorViewList){
            actorViewList.add(actorView)
        }
        return actorViewList
    }

    fun getBackgroundTheme() : Int{
        return backgroundTheme
    }

    fun getRecord() : JSONObject {
        return record
    }

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

    fun endRecordTime(time : Long) {
        endRecordTime = time
        onTouchBoMoSceneListener.endRecordTime(time)
    }

    fun saveRecord(){
        isRecorded = true
        record = onTouchBoMoSceneListener.getRecord()
        playView.bringToFront()
    }

    fun getIsRecord() : Boolean {
        return isRecorded
    }

    fun setonClickPlayListener(onClickListener: OnClickListener){
        if (isRecorded) playIcon.setOnClickListener(onClickListener)
    }

    fun setRecord(record : JSONObject){
        this.record = record
    }

    fun setPlayViewGone(){
        playView.visibility = View.INVISIBLE
    }

    fun setPlayViewVis(){
        playView.visibility = View.VISIBLE
    }
}