package com.bomostory.sceneeditmodule.view

import android.content.Context
import android.graphics.drawable.Drawable
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.util.Log
import com.bomostory.sceneeditmodule.basicdata.Scene
import kotlin.math.pow

class SceneView1 : ConstraintLayout {

    var backgroundView: LayerView? = null

    var scene: Scene? = null
        set(value) {
            field = value
            value?.let {
                removeAllViews()

                backgroundView = LayerView(context)
                backgroundView?.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)
                backgroundView?.background = Drawable.createFromPath(it.backgroundPath)
                addView(backgroundView)

                for (layer in it.layers) {
                    val layerView = LayerView(context)
                    layerView.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)
                    layerView.id = layer.id
                    layerView.editActors = layer.actors
                    addView(layerView)
                }
            }
        }

    var x: Int = 0
        set(value) {
            field = value
            scene?.let {

                backgroundView?.let {
                    it.layout(
                            (value / 2f.pow(5)).toInt(),
                            it.y.toInt(),
                            (value / 2f.pow(5)).toInt() + it.width,
                            it.y.toInt() + it.height
                    )
                }

                for (layer in it.layers) {
                    val layerView = findViewById<LayerView>(layer.id)

                    layerView.layout(
                            value / ((2f.pow(it.layers.indexOf(layer))).toInt()),
                            layerView.y.toInt(),
                            value / ((2f.pow(it.layers.indexOf(layer))).toInt()) + layerView.width,
                            layerView.y.toInt() + layerView.height
                    )
                }
            }
        }

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle)

}