package com.bomostory.sceneeditmodule

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.support.test.InstrumentationRegistry
import android.support.test.rule.GrantPermissionRule
import com.bomostory.sceneeditmodule.MovieEditActivity.Companion.FPS
import com.bomostory.sceneeditmodule.basicdata.Scene
import com.bomostory.sceneeditmodule.basicdata.Story
import com.example.exportmedia.MediaHelper
import com.example.exportmedia.vedio.MovieMaker
import com.example.tfat.myapplication.R
import com.google.gson.Gson
import io.reactivex.Observable
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test

import java.util.AbstractMap
import kotlin.math.pow

class ExportMovieFilmsTest {

    private lateinit var context: Context

    private lateinit var scene: Scene

    private val mediaHelper = MediaHelper()

    private val story = Gson().fromJson<Story>("{\"scenes\":[{\"backgroundPath\":\"/storage/emulated/0/Android/yuheng/public/scene/s3/scene_sample03_small.png\",\"layers\":[{\"actors\":[],\"id\":0,\"name\":\"layer0\"},{\"actors\":[{\"positionX\":1084,\"positionY\":297,\"resourcePath\":\"/storage/emulated/0/Android/yuheng/public/actor/bat/act1/actor_bat_p1_small.png\",\"sideLength\":200}],\"id\":1,\"name\":\"layer1\"},{\"actors\":[{\"positionX\":951,\"positionY\":5,\"resourcePath\":\"/storage/emulated/0/Android/yuheng/public/actor/bat/act1/actor_bat_p1_small.png\",\"sideLength\":200}],\"id\":2,\"name\":\"layer2\"},{\"actors\":[{\"positionX\":909,\"positionY\":446,\"resourcePath\":\"/storage/emulated/0/Android/yuheng/public/actor/ant/act1/actor_ant_p1_small.png\",\"sideLength\":200},{\"positionX\":795,\"positionY\":404,\"resourcePath\":\"/storage/emulated/0/Android/yuheng/public/actor/ant/act1/actor_ant_p1_small.png\",\"sideLength\":200}],\"id\":3,\"name\":\"layer3\"},{\"actors\":[{\"positionX\":1738,\"positionY\":342,\"resourcePath\":\"/storage/emulated/0/Android/yuheng/public/actor/cat_black/act1/actor_catblack_p2_small.png\",\"sideLength\":200}],\"id\":4,\"name\":\"layer4\"}],\"record\":{\"period\":11164,\"tracks\":[{\"positionX\":-3,\"time\":1170},{\"positionX\":-11,\"time\":1188},{\"positionX\":-24,\"time\":1204},{\"positionX\":-41,\"time\":1223},{\"positionX\":-55,\"time\":1238},{\"positionX\":-70,\"time\":1254},{\"positionX\":-88,\"time\":1279},{\"positionX\":-94,\"time\":1298},{\"positionX\":-98,\"time\":1316},{\"positionX\":-101,\"time\":1333},{\"positionX\":-104,\"time\":1371},{\"positionX\":-107,\"time\":1388},{\"positionX\":-112,\"time\":1408},{\"positionX\":-120,\"time\":1426},{\"positionX\":-132,\"time\":1445},{\"positionX\":-147,\"time\":1463},{\"positionX\":-161,\"time\":1480},{\"positionX\":-179,\"time\":1496},{\"positionX\":-193,\"time\":1514},{\"positionX\":-205,\"time\":1534},{\"positionX\":-219,\"time\":1554},{\"positionX\":-228,\"time\":1570},{\"positionX\":-237,\"time\":1587},{\"positionX\":-244,\"time\":1607},{\"positionX\":-248,\"time\":1626},{\"positionX\":-251,\"time\":1649},{\"positionX\":-255,\"time\":1664},{\"positionX\":-258,\"time\":1679},{\"positionX\":-265,\"time\":1696},{\"positionX\":-270,\"time\":1715},{\"positionX\":-280,\"time\":1733},{\"positionX\":-289,\"time\":1752},{\"positionX\":-300,\"time\":1770},{\"positionX\":-308,\"time\":1787},{\"positionX\":-318,\"time\":1807},{\"positionX\":-325,\"time\":1824},{\"positionX\":-336,\"time\":1843},{\"positionX\":-347,\"time\":1861},{\"positionX\":-354,\"time\":1879},{\"positionX\":-361,\"time\":1898},{\"positionX\":-365,\"time\":1914},{\"positionX\":-373,\"time\":1936},{\"positionX\":-376,\"time\":1951},{\"positionX\":-380,\"time\":1969},{\"positionX\":-383,\"time\":1986},{\"positionX\":-387,\"time\":2006},{\"positionX\":-390,\"time\":2024},{\"positionX\":-392,\"time\":2042},{\"positionX\":-396,\"time\":2063},{\"positionX\":-392,\"time\":2224},{\"positionX\":-389,\"time\":2241},{\"positionX\":-386,\"time\":2259},{\"positionX\":-382,\"time\":2276},{\"positionX\":-380,\"time\":2296},{\"positionX\":-379,\"time\":2314},{\"positionX\":-375,\"time\":2334},{\"positionX\":-373,\"time\":2368},{\"positionX\":-369,\"time\":2386},{\"positionX\":-366,\"time\":2425},{\"positionX\":-363,\"time\":2443},{\"positionX\":-360,\"time\":2478},{\"positionX\":-356,\"time\":2530},{\"positionX\":-353,\"time\":2587},{\"positionX\":-350,\"time\":2641},{\"positionX\":-347,\"time\":2713},{\"positionX\":-347,\"time\":2748},{\"positionX\":-341,\"time\":2767},{\"positionX\":-340,\"time\":2822},{\"positionX\":-337,\"time\":2857},{\"positionX\":-334,\"time\":2876},{\"positionX\":-329,\"time\":2896},{\"positionX\":-327,\"time\":2915},{\"positionX\":-322,\"time\":2933},{\"positionX\":-319,\"time\":2950},{\"positionX\":-315,\"time\":2966},{\"positionX\":-314,\"time\":2984},{\"positionX\":-311,\"time\":3022},{\"positionX\":-308,\"time\":3059},{\"positionX\":-305,\"time\":3095},{\"positionX\":-301,\"time\":3112},{\"positionX\":-298,\"time\":3167},{\"positionX\":-295,\"time\":3184},{\"positionX\":-288,\"time\":3223},{\"positionX\":-285,\"time\":3240},{\"positionX\":-281,\"time\":3257},{\"positionX\":-273,\"time\":3275},{\"positionX\":-267,\"time\":3292},{\"positionX\":-256,\"time\":3314},{\"positionX\":-248,\"time\":3330},{\"positionX\":-238,\"time\":3349},{\"positionX\":-231,\"time\":3367},{\"positionX\":-224,\"time\":3383},{\"positionX\":-216,\"time\":3405},{\"positionX\":-212,\"time\":3421},{\"positionX\":-207,\"time\":3438},{\"positionX\":-202,\"time\":3457},{\"positionX\":-199,\"time\":3478},{\"positionX\":-195,\"time\":3494},{\"positionX\":-192,\"time\":3510},{\"positionX\":-191,\"time\":3529},{\"positionX\":-186,\"time\":3546},{\"positionX\":-182,\"time\":3565},{\"positionX\":-178,\"time\":3583},{\"positionX\":-175,\"time\":3607},{\"positionX\":-172,\"time\":3621},{\"positionX\":-165,\"time\":3639},{\"positionX\":-160,\"time\":3659},{\"positionX\":-157,\"time\":3675},{\"positionX\":-153,\"time\":3694},{\"positionX\":-152,\"time\":3713},{\"positionX\":-149,\"time\":3747},{\"positionX\":-146,\"time\":3764},{\"positionX\":-141,\"time\":3783},{\"positionX\":-139,\"time\":3800},{\"positionX\":-134,\"time\":3826},{\"positionX\":-131,\"time\":3839},{\"positionX\":-126,\"time\":3875},{\"positionX\":-124,\"time\":3895},{\"positionX\":-123,\"time\":3911},{\"positionX\":-120,\"time\":3947},{\"positionX\":-117,\"time\":3964},{\"positionX\":-111,\"time\":3983},{\"positionX\":-106,\"time\":4000},{\"positionX\":-101,\"time\":4018},{\"positionX\":-97,\"time\":4037},{\"positionX\":-93,\"time\":4056},{\"positionX\":-89,\"time\":4075},{\"positionX\":-87,\"time\":4092},{\"positionX\":-81,\"time\":4129},{\"positionX\":-77,\"time\":4148},{\"positionX\":-72,\"time\":4164},{\"positionX\":-68,\"time\":4187},{\"positionX\":-63,\"time\":4201},{\"positionX\":-56,\"time\":4218},{\"positionX\":-55,\"time\":4254},{\"positionX\":-52,\"time\":4310},{\"positionX\":-49,\"time\":4365},{\"positionX\":-40,\"time\":4400},{\"positionX\":-31,\"time\":4418},{\"positionX\":-25,\"time\":4438},{\"positionX\":-20,\"time\":4456},{\"positionX\":-13,\"time\":4477},{\"positionX\":-10,\"time\":4494},{\"positionX\":-7,\"time\":4510},{\"positionX\":-6,\"time\":4527},{\"positionX\":-3,\"time\":4544},{\"positionX\":0,\"time\":4565},{\"positionX\":8,\"time\":4599},{\"positionX\":14,\"time\":4635},{\"positionX\":16,\"time\":4655},{\"positionX\":19,\"time\":4676},{\"positionX\":23,\"time\":4694},{\"positionX\":29,\"time\":4728},{\"positionX\":32,\"time\":4745},{\"positionX\":40,\"time\":4780},{\"positionX\":42,\"time\":4798},{\"positionX\":45,\"time\":4818},{\"positionX\":49,\"time\":4855},{\"positionX\":52,\"time\":4874},{\"positionX\":55,\"time\":4925},{\"positionX\":58,\"time\":4998},{\"positionX\":62,\"time\":5091},{\"positionX\":65,\"time\":5180},{\"positionX\":68,\"time\":5363},{\"positionX\":65,\"time\":7605},{\"positionX\":61,\"time\":7658},{\"positionX\":58,\"time\":7694},{\"positionX\":51,\"time\":7735},{\"positionX\":39,\"time\":7754},{\"positionX\":26,\"time\":7772},{\"positionX\":1,\"time\":7789},{\"positionX\":-15,\"time\":7807},{\"positionX\":-37,\"time\":7825},{\"positionX\":-55,\"time\":7844},{\"positionX\":-70,\"time\":7863},{\"positionX\":-89,\"time\":7880},{\"positionX\":-99,\"time\":7897},{\"positionX\":-108,\"time\":7914},{\"positionX\":-116,\"time\":7933},{\"positionX\":-121,\"time\":7952},{\"positionX\":-124,\"time\":7969},{\"positionX\":-120,\"time\":8058},{\"positionX\":-117,\"time\":8076},{\"positionX\":-114,\"time\":8096},{\"positionX\":-109,\"time\":8117},{\"positionX\":-102,\"time\":8132},{\"positionX\":-100,\"time\":8152},{\"positionX\":-95,\"time\":8170},{\"positionX\":-94,\"time\":8204},{\"positionX\":-91,\"time\":8239},{\"positionX\":-88,\"time\":8348},{\"positionX\":-84,\"time\":8777},{\"positionX\":-81,\"time\":8813},{\"positionX\":-78,\"time\":8833},{\"positionX\":-75,\"time\":8868},{\"positionX\":-71,\"time\":8941},{\"positionX\":-68,\"time\":8976},{\"positionX\":-63,\"time\":8997},{\"positionX\":-56,\"time\":9011},{\"positionX\":-43,\"time\":9030},{\"positionX\":-32,\"time\":9048},{\"positionX\":-15,\"time\":9066},{\"positionX\":-1,\"time\":9084},{\"positionX\":9,\"time\":9103},{\"positionX\":10,\"time\":9123},{\"positionX\":14,\"time\":9140},{\"positionX\":16,\"time\":9175},{\"positionX\":16,\"time\":9245},{\"positionX\":19,\"time\":9299},{\"positionX\":23,\"time\":9335},{\"positionX\":26,\"time\":9389},{\"positionX\":29,\"time\":9444},{\"positionX\":32,\"time\":9481},{\"positionX\":35,\"time\":9515},{\"positionX\":39,\"time\":9533},{\"positionX\":42,\"time\":9551},{\"positionX\":45,\"time\":9587},{\"positionX\":53,\"time\":9623},{\"positionX\":58,\"time\":9661},{\"positionX\":61,\"time\":9679},{\"positionX\":66,\"time\":9694},{\"positionX\":69,\"time\":9714},{\"positionX\":71,\"time\":9733},{\"positionX\":75,\"time\":9750},{\"positionX\":78,\"time\":9785},{\"positionX\":81,\"time\":9821},{\"positionX\":84,\"time\":9857},{\"positionX\":87,\"time\":9875},{\"positionX\":91,\"time\":9928},{\"positionX\":94,\"time\":10017},{\"positionX\":97,\"time\":10071},{\"positionX\":100,\"time\":10125},{\"positionX\":100,\"time\":11164}]}}]}", Story::class.java)

    @get:Rule
    var permissionRule = GrantPermissionRule.grant(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)!!

    @Before
    fun setUp() {

        context = InstrumentationRegistry.getTargetContext().applicationContext

        scene = story.scenes[0]

        mediaHelper.init(context)
    }

    @After
    fun tearDown() {
    }

    @Test
    fun output() {
        val movieBuilder = MovieMaker.Builder(mediaHelper)
        var preBitmap: Bitmap?

        Observable.create<AbstractMap.SimpleEntry<Int, Bitmap>> {
            scene.apply {
                record?.apply {
                    var bitmap = drawScene(0)
                    var trackPosition = -1
                    var bitmapIndex = 0

                    for (t in 0..period step ((1f / FPS.toFloat()) * 1000).toLong()) {
                        for (track in tracks) {
                            if (t >= track.time && tracks.indexOf(track) > trackPosition) {
                                preBitmap = bitmap
                                preBitmap?.recycle()
                                bitmap = drawScene(track.positionX)
                                trackPosition = tracks.indexOf(track)
                                break
                            }
                        }
                        bitmap?.apply {
                            println("index : $bitmapIndex t : $t")
                            movieBuilder.addImage(bitmapIndex++, this).subscribe()
                        }
                    }
                    it.onComplete()
                }
            }
        }.subscribe({}, {
            it.printStackTrace()
        })
    }

    private fun drawScene(trackX: Int): Bitmap? {
        var sceneBitmap: Bitmap? = null
        val scaledWidth = context.resources.getDimensionPixelSize(R.dimen.movie_width)
        val scaleHeight = context.resources.getDimensionPixelSize(R.dimen.movie_height)

        scene?.apply {
            sceneBitmap = BitmapFactory.decodeFile(backgroundPath)
            sceneBitmap = Bitmap.createScaledBitmap(sceneBitmap, scaledWidth, scaleHeight, true)

            val canvas = Canvas(sceneBitmap)
            for (layer in layers) {
                for (actor in layer.actors) {
                    var actorBitmap = BitmapFactory.decodeFile(actor.resourcePath)
                    actorBitmap = Bitmap.createScaledBitmap(actorBitmap, actor.sideLength, actor.sideLength, true)

                    canvas.save()
                    canvas.translate(trackX / 2f.pow(layers.indexOf(layer)), 0f)
                    canvas.drawBitmap(actorBitmap, actor.positionX.toFloat(), actor.positionY.toFloat(), Paint())
                    canvas.restore()

                    actorBitmap.recycle()
                }
            }
        }
        return sceneBitmap
    }
}