package jp.kazhida.kotos2d

import android.app.Activity
import android.content.Context
import android.os.Build
import android.util.Log
import android.os.Message
import android.os.Bundle
import android.widget.FrameLayout
import android.view.ViewGroup

/**
 * Created by kazhida on 2013/07/27.
 */
public abstract class Kotos2dActivity : Activity(), Kotos2dHelper.HelperListener {

    class object {
        val TAG = javaClass<Kotos2dActivity>().getSimpleName()
        var context: Context? = null
        val isAndroidEmulator: Boolean
            get() {
                val model   = Build.MODEL
                val product = Build.PRODUCT
                Log.d(TAG, "model=" + model + ", product=" + product)
                return product.equals("sdk") || product.contains("_sdk") || product.contains("sdk_")
            }
    }

    var surfaceView: Kotos2dGLSurfaceView? = null
    val handler = Kotos2dHandler(this)

    open fun onCreateView(): Kotos2dGLSurfaceView {
        return Kotos2dGLSurfaceView(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super<Activity>.onCreate(savedInstanceState)

        context = this

        val MP = ViewGroup.LayoutParams.MATCH_PARENT
        val WC = ViewGroup.LayoutParams.WRAP_CONTENT

        val frameLayout = FrameLayout(this)

        //todo: EditText関連

        surfaceView = onCreateView();
        surfaceView?.renderer = Kotos2dRenderer()
        if (isAndroidEmulator) surfaceView?.setEGLConfigChooser(8, 8, 8, 8, 16, 0)

        setContentView(frameLayout, ViewGroup.LayoutParams(MP, MP))

        Kotos2dHelper.init(this, this)
    }

    override fun onResume() {
        super<Activity>.onResume()
        Kotos2dHelper.sharedInstance().onResume()
        surfaceView?.onResume()
    }

    override fun onPause() {
        super<Activity>.onPause()
        Kotos2dHelper.sharedInstance().onPause()
        surfaceView?.onPause()
    }

    override fun runOnGLThread(runnable: Runnable) {
        surfaceView?.queueEvent(runnable)
    }

    override fun showDialog(title: String, message: String) {
        val msg  = Message()
        msg.what = Kotos2dHandler.HANDLE_SHOW_DIALOG
        msg.obj  = Kotos2dHandler.DialogMessage(title, message)
        handler.sendMessage(msg)
    }
}
