
var results = new Phaser.Class({
       Extends: Phaser.Scene,
        initialize: function() {
           Phaser.Scene.call(this, { "key": "results" });
        },
        init: function(data) {
            this.score = data.score
            this.nam = data.nam
            this.time = data.time
        },
        preload: function() {   
         
        },
        create: function() {
            const self = this
           
            gr = this.add.image(400,300,"back")

            log = this.add.image(400,300,"logo")
            log.setAlpha(0.5)

            self.full = new fullButton(self,gr.displayWidth - 34, gr.displayHeight - 587, "fulls",'game')
            this.add.existing(self.full)

            this.exi = new exitButton(this,ground.displayWidth - 19, ground.displayHeight - 587,"exit")
            this.add.existing(this.exi)

            congration = this.add.text(200,200,"Congratulations "+nam+"!", {font: "30px Optima", fill: "#000000"})
            congort = this.add.text(200,211,"you scored "+score+" points \n and it only took you "+time+"!", {font: "30px Optima", fill: "#000000"})
            congration.setAlign("center")
            congration.setColor("#000000")
            congration.setOrigin(0.5)

            congort.setAlign("center")
            congort.setFontSize(25)
            congort.setColor("#000000")
            congort.setOrigin(0.5)
            
            congration.setPosition(400,200)
            congort.setPosition(400,235)

            this.sendScore(score,nam,time)

            leadBut = new leaderButton(self,400,400,'rest',2,1)
            this.add.existing(leadBut)
            tes = this.add.text(2,2,"Leaderboard", {font: "25px Optima", fill: "#000000"})
            Phaser.Display.Align.In.Center(tes,leadBut)

            retBut = new returnButton(self,400,500,'rest',2,1)
            this.add.existing(retBut)
            te = this.add.text(1,1,"Title", {font: "25px Optima", fill: "#000000"})
            te.setStyle({
                color:"#000000",
                fontSize: "25px",
                fontWeight: 'bold'
            })
            Phaser.Display.Align.In.Center(te,retBut)
        },
        update: function() {
            const self = this
            onclick = function(event) {
                if(event.clientX > 725 && event.clientX < 795){
                    if(event.clientY > 5 && event.clientY < 75){
                        self.scene.start("leader")
                    }
                }
            }
        },
        async sendScore(score,nam,time){
            try{
                
                const path = '/~/Jeopardy/open/leaders';
	            const method = 'POST';
	            const headers = { 'Content-Type': 'application/json' }
	            const billy = nam;
	            const body = JSON.stringify({ billy, score, time });
	
	            await fetch(path, { method, headers, body });
            }
            catch{
                console.log('uh oh!!')
            }
        }

});