package sample

import javafx.application.Platform
import javafx.scene.Cursor
import javafx.scene.Group
import javafx.scene.Scene
import javafx.scene.canvas.Canvas
import javafx.scene.canvas.GraphicsContext
import javafx.scene.input.KeyCombination
import javafx.scene.paint.Color
import javafx.scene.shape.ArcType
import javafx.scene.text.Font
import javafx.stage.Stage
import kotlin.concurrent.thread
import kotlin.math.atan
import kotlin.math.sin
import kotlin.system.exitProcess


class demo() {

    var height = 1080
    var width = 1920

    var yPercent = 0.0
    var xPercent = 0.0

    var tickLenght = 20
    var demoLenght = 10000

    var breakValue = 73


    var atTick = 0

    var canvas: Canvas? = null

    var givenStage: Stage? = null


    fun start(stage: Stage) {

        val root = Group()

        givenStage = stage




        println(stage.width)

        initSizeValues()

        fillWithBlack()

        stage.show()

        root.children.add(canvas)

        stage.scene = Scene(root)







        thread {

            synth().playAudio()
        }


        thread {


            for (i in 0..500) {

                if (i < breakValue) {
                    Thread.sleep(30)

                    //Load sounds here


                    Platform.runLater(Runnable {
                        loadScreen(i)


                        drawScene()

                        //Update UI here
                    })

                } else if (i == breakValue) {


                    //Screen1
                    Platform.runLater(Runnable {

                        //stage.isFullScreen = true

                        stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH)
                        stage.isFullScreen = true
                        loadScreen(5)
                        glitchScreen()


                        drawScene()

                        //Update UI here
                    })

                    Thread.sleep(4000)


                    //Blackscreen
                    Platform.runLater(Runnable {

                        //stage.isFullScreen = true
                        fillWithBlack()

                        drawScene()
                        //Update UI here
                    })
                    Thread.sleep(1000)


                    //Bluescreen

                    var bluescreenSleepTick = 600

                    for (i in 0..25) {
                        Platform.runLater(Runnable {

                            //stage.isFullScreen = true


                            windowsDeathScreen(i.toDouble())
                            drawScene()

                            //Update UI here


                            if (i >= 5) {

                                bluescreenSleepTick = 20
                                windowsDeathScreen(i.toDouble())
                            }

                        })

                        Thread.sleep(bluescreenSleepTick.toLong())

                    }


                    //Demo b


                }


            }

            //New part


            //New part


            for (i in 0..8000) {


                Platform.runLater(Runnable {


                    demoBegins(i)


                    drawScene()


                })
                if (i == 2000) {



                }

                Thread.sleep(10)

            }

            exitProcess(0)
        }


    }

    fun demoBegins(atFrame: Int) {

        fillWithBlack()

        val tgc = canvas!!.graphicsContext2D




        /*
        if (atFrame < 200) {
            var textOpacity = atFrame / 300.0

            var extrapos = (atFrame / 1200.0) * (50.0 * xPercent)

            if (textOpacity > 1) {
                textOpacity = 1.0
            }

            tgc.fill = Color.gray(textOpacity)

            tgc.fillText("Crashes and forbidden photons brought by...", xPercent * 10 + extrapos, yPercent * 20)


            tgc.fill = Color.gray(0.0)

            tgc.fillRect(xPercent * 30, yPercent * 15, 200.0, 50.0)

        }
        */




        if (atFrame < 4000) {

            //Triangle animation


            var percentDone = 100 * ((atFrame) / (4000.0))


            tgc.fill = Color.gray(0.5)
            tgc.stroke = Color.gray(0.5)

            tgc.lineWidth = xPercent

            var startpointx = 0.0
            var startpointy = yPercent * 100


            var line1Percent = percentDone
            if (line1Percent > 10) {
                line1Percent = 10.0
            }

            var line1LengthMultiplier = line1Percent / 10.0


            tgc.stroke = Color.WHITE

            tgc.strokeLine(startpointx, startpointy, startpointx + xPercent * 50 * line1LengthMultiplier, startpointy - yPercent * 50 * line1LengthMultiplier)

            //Line2 here


            var line2Percent = (percentDone - 10)

            var line2LenghtMultiplier = line2Percent / 10

            if (percentDone >= 10.0) {


                tgc.stroke = Color.WHITE
                tgc.strokeLine(xPercent * 50.0, yPercent * 50.0, xPercent * 50.0 + xPercent * 50 * line2LenghtMultiplier, yPercent * 50 + yPercent * 50 * line2LenghtMultiplier)



               // tgc.strokeLine(40.7, 40.7, 30.0, 324.0)

                tgc.lineWidth = 1.0

                tgc.stroke = Color.gray(0.5)


                tgc.strokeLine(xPercent * 50, yPercent * 50, xPercent * 50 + xPercent * 50 * line2LenghtMultiplier, yPercent * 50)

                tgc.strokeLine(xPercent * 50, yPercent * 50, xPercent * 50 - xPercent * 50 * line2LenghtMultiplier, yPercent * 50)


            }

            tgc.lineWidth = 4.0


            //Mask drawing happens here

            tgc.fill = Color.BLACK
            tgc.fillRect(xPercent * 48.0, yPercent * 48.0, xPercent * 4, yPercent * 2)


            //Aftermask


            //DRawing purple line
            if (percentDone > 10 && percentDone < 16) {

                var line3Progress = (percentDone - 10) / 2
                tgc.lineWidth = yPercent * 4
                tgc.stroke = Color.rgb(204, 0, 204, 0.80)
                tgc.strokeLine(xPercent * 50, yPercent * 48, xPercent * 50, yPercent * 50 - yPercent * 20 * line3Progress)

            }



            if (percentDone >= 16) {

                var blackSlider = percentDone - 16

                if (blackSlider > 1) {

                    blackSlider = 1.0
                }



                tgc.fill = Color.grayRgb(0, blackSlider)

                tgc.fillRect(0.0, 0.0, xPercent * 100, yPercent * 100)


            }

            if (percentDone >= 16 && percentDone < 25) {


                var rainbowSlider = (percentDone - 16) * 50


                tgc.fill = Color.hsb(rainbowSlider, 0.9, 1.0, 1.0)

                tgc.font = Font.font(50.0)

                tgc.fillText("Crashes and forbidden lightray by...", 10 * xPercent, yPercent * 45)


            }



            //"Taps"
            if (percentDone > 25 && percentDone < 35) {


                var rainbowSlider = (percentDone - 16) * 50

                tgc.fill = Color.hsb(rainbowSlider, 0.9, 1.0, 1.0)
                tgc.font = Font.font(225.0)


                var positionSliderX = (percentDone - 13) * 5


                val deltaRad = Math.PI / 2



                tgc.fillText("La", -50 * xPercent + xPercent * 2 * sin(positionSliderX), yPercent * 50)

                tgc.fillText("Ry", -30 * xPercent + xPercent * 2 * sin(positionSliderX), yPercent * 50)


                tgc.fillText("Zy", -10 * xPercent + xPercent * 2 * sin(positionSliderX), yPercent * 50)




            }






            //Final text
            if (percentDone > 30) {

                var greyslider = percentDone - 16

                if (greyslider > 1) greyslider = 1.0


                var rainbowSlider = (percentDone - 16) * 50


                tgc.fill = Color.grayRgb(255, greyslider)

                tgc.fillRect(0.0, 0.0, xPercent * 100, yPercent * 100)

                tgc.fill = Color.hsb(rainbowSlider, 0.9, 1.0, 1.0)
                tgc.font = Font.font(225.0)


                var locationSlider = percentDone - 30


                val deltaRad = Math.PI / 2

                tgc.fillText("La", -50 * xPercent + xPercent * 50 * atan(locationSlider * 30), yPercent * 50)

                tgc.fillText("Ry", -30 * xPercent + xPercent * 50 * atan(locationSlider * 30 - 50), yPercent * 50)


                tgc.fillText("Zy", -10 * xPercent + xPercent * 50 * atan(locationSlider * 30 - 100), yPercent * 50)


                tgc.fillText("(R-Z)", -10 * xPercent + xPercent * 50 * atan(locationSlider * 30 - 120), yPercent * 75)


            }

            if (percentDone > 35){
                exitProcess(0)
            }


        }


    }

    fun drawScene() {

        (givenStage as Stage).scene.cursor = Cursor.NONE
        (givenStage as Stage).show()

    }


    fun glitchScreen() {

        val tgc = (canvas as Canvas).graphicsContext2D

        val lineWidth = xPercent / 8
        var atLine = 0.0

        for (i in 0..800) {

            tgc.fill = Color.rgb((Math.random() * 255).toInt(), (Math.random() * 255).toInt(), (Math.random() * 255).toInt())

            tgc.fillRect(atLine, 0.0, atLine + lineWidth, height.toDouble())

            atLine += lineWidth


        }


        for (i in 0..800) {

            tgc.fill = Color.rgb((Math.random() * 255).toInt(), (Math.random()).toInt(), (Math.random()).toInt())

            tgc.fillRect(xPercent * 15, 0.0, xPercent * 18, height.toDouble())

            atLine += lineWidth


        }


    }

    fun windowsDeathScreen(percent: Double) {


        val tgc = (canvas as Canvas).graphicsContext2D

        tgc.fill = Color.rgb(0, 128, 255)

        tgc.fillRect(0.0, 0.0, width.toDouble(), height.toDouble())

        tgc.fill = Color.WHITE

        tgc.font = Font.font(200.0)
        tgc.fillText(":(", 20.0, yPercent * 30)


        tgc.font = (Font.font(30.0))

        tgc.fillText("Your PC ran into a problem and needs to restart. We're just collecting some error", 20.0, yPercent * 40)
        tgc.fillText("info, and then we'll restart for you($percent% complete)", 20.0, yPercent * 45)


        tgc.font = Font.font(20.0)
        tgc.fillText("If you'd like to know more, you can search online later for this error VIDEO_TDR_FAILURE(nvlddmkm.sys)", 20.0, yPercent * 55)



        if (percent >= 5.0) {

            tgc.font = Font.font(200.0)

            //tgc.canvas.rotate = sin((percent-13)/100)*100


            //tgc.canvas.rotate = sin((percent-5)/100)*100


            //tgc.canvas.transforms.add(Transform.scale(1.1, 1.1))


            tgc.fillText("Just kidding!", 20.0, yPercent * 50)
        }

    }


    fun initSizeValues() {

        width = givenStage!!.width.toInt()
        height = givenStage!!.height.toInt()

        yPercent = height / 100.0
        xPercent = width / 100.0

        canvas = Canvas(width.toDouble(), height.toDouble())

        /*
        canvas?.height = height.toDouble()
        canvas?.width = height.toDouble()*/

    }


    private fun loadScreen(at: Int) {
        //println("Loading")

        val tgc = canvas!!.graphicsContext2D

        fillWithBlack()

        tgc.stroke = Color.WHITE
        tgc.strokeRect(xPercent * 10, yPercent * 45, 80 * xPercent, yPercent * 10)

        tgc.fill = Color.rgb(255, 119, 0)

        tgc.fillRect(xPercent * 11, yPercent * 46, (at / 100.0) * 78.0 * xPercent, yPercent * 8)


    }


    private fun drawFrame() {

        atTick++
        //   clearGC()
        fillWithBlack()

        val tgc = canvas!!.graphicsContext2D

        tgc.fill = Color.RED
        tgc.stroke = Color.PINK
        tgc.lineWidth = 2.0
        tgc.strokeLine(4.0 + atTick, 102.0 * (-atTick), 20.0, 4.0)


    }


    private fun clearGC() {


        // (canvas!!.graphicsContext2D).clearRect(0.0, 0.0, width.toDouble(), height.toDouble())
    }


    private fun fillWithBlack() {

        val tgc = (canvas as Canvas).graphicsContext2D

        tgc.fill = Color.BLACK

        tgc.fillRect(0.0, 0.0, width.toDouble(), height.toDouble())

        //println("$width from fill with black, $height from fill with black")
    }


    private fun drawShapes2(gc: GraphicsContext) {
        gc.fill = Color.RED
        gc.stroke = Color.PINK
        gc.lineWidth = 2.0
        gc.strokeLine(4.0, 102.0, 710.0, 4.0)
        gc.fillOval(10.0, 60.0, 40.0, 50.0)
        gc.strokeOval(60.0, 60.0, 30.0, 30.0)

    }


    private fun drawShapes(gc: GraphicsContext) {
        gc.fill = Color.GREEN
        gc.stroke = Color.BLUE
        gc.lineWidth = 5.0
        gc.strokeLine(40.0, 10.0, 10.0, 40.0)
        gc.fillOval(10.0, 60.0, 30.0, 30.0)
        gc.strokeOval(60.0, 60.0, 30.0, 30.0)
        gc.fillRoundRect(110.0, 60.0, 30.0, 30.0, 10.0, 10.0)
        gc.strokeRoundRect(160.0, 60.0, 30.0, 30.0, 10.0, 10.0)
        gc.fillArc(10.0, 110.0, 30.0, 30.0, 45.0, 240.0, ArcType.OPEN)
        gc.fillArc(60.0, 110.0, 30.0, 30.0, 45.0, 240.0, ArcType.CHORD)
        gc.fillArc(110.0, 110.0, 30.0, 30.0, 45.0, 240.0, ArcType.ROUND)
        gc.strokeArc(10.0, 160.0, 30.0, 30.0, 45.0, 240.0, ArcType.OPEN)
        gc.strokeArc(60.0, 160.0, 30.0, 30.0, 45.0, 240.0, ArcType.CHORD)
        gc.strokeArc(110.0, 160.0, 30.0, 30.0, 45.0, 240.0, ArcType.ROUND)
        gc.fillPolygon(doubleArrayOf(10.0, 40.0, 10.0, 40.0),
                doubleArrayOf(210.0, 210.0, 240.0, 240.0), 4)
        gc.strokePolygon(doubleArrayOf(60.0, 90.0, 60.0, 90.0),
                doubleArrayOf(210.0, 210.0, 240.0, 240.0), 4)
        gc.strokePolyline(doubleArrayOf(110.0, 140.0, 110.0, 140.0),
                doubleArrayOf(210.0, 210.0, 240.0, 240.0), 4)
    }

}