//var gl = new WebGL2RenderingContext(); //enable WebGL 2 partial support
const settings = new Settings();
settings.demo.sync.rocketFile = 'demo.rocket';
settings.demo.sync.beatsPerMinute = 115;
settings.demo.sync.rowsPerBeat = 8;



Demo.prototype.init = function()
{
    const settings = new Settings();

    var start = 0;
    var duration = 100;
    var layer = 1; 

    this.loader.addAnimation(
    {
        "start": 0, "duration": duration, "layer": layer
        ,"image": "_embedded/defaultWhite.png",
        color: [
            {
              r: 0,
              g: 0,
              b: 0
            }
          ],
    });





    this.loader.addAnimation(
        {
            "start": 0, "duration": duration, "layer": layer
            ,"image": "_embedded/defaultWhite.png",
            scale: [{ uniform3d: () => (getSceneTimeFromStart()%0.288)*0.3+0.6 }],
            color: [
                {
                  r: 0,
                  g: 0,
                  b: 0
                }
              ],
        });

    this.loader.addAnimation(
        {
            "start": 0, "duration": duration, "layer": layer
            ,"image": "_embedded/defaultWhite.png",
            scale: [{ uniform3d: 0.2 }],
            color: [
                {
                  r: 0,
                  g: 0,
                  b: 0
                }
              ],
              position: [
                {
                  x: () => 0.3*Math.sin(Math.sin(getSceneTimeFromStart()*5)),
                  y: () => 0.3*Math.sin(Math.sin(getSceneTimeFromStart()*5)),
                  z: () => 0
                }
              ]
        });
        this.loader.addAnimation(
            {
                "start": 0, "duration": duration, "layer": layer
                ,"image": "_embedded/defaultWhite.png",
                scale: [{ uniform3d: 0.2 }],
                color: [
                    {
                      r: 0,
                      g: 0,
                      b: 0
                    }
                  ],
                  position: [
                    {
                      x: () => 0.3*Math.sin(Math.sin(getSceneTimeFromStart()*5)),
                      y: () => -0.3*Math.sin(Math.sin(getSceneTimeFromStart()*5)),
                      z: () => 0
                    }
                  ]
            });
            
            for(let i = 0; i<2; i++)
            {
                this.loader.addAnimation(
                    {
                        "start": 0, "duration": duration, "layer": layer
                        ,"image": "_embedded/defaultWhite.png",
                        scale: [{ uniform3d: 0.2 }],
                        color: [
                            {
                            r: 0,
                            g: 0,
                            b: 0,
                            a: () => Math.sin(getSceneTimeFromStart()*4+i) 
                            }
                        ],
                        position: [
                            {
                            x: () => 0.4 - (0.8 * i),
                            y: () => 0.4,
                            z: () => 0
                            }
                        ]
                    });
            }
            for(let i = 0; i<2; i++)
                {
                    this.loader.addAnimation(
                        {
                            "start": 0, "duration": duration, "layer": layer
                            ,"image": "_embedded/defaultWhite.png",
                            scale: [{ uniform3d: 0.2 }],
                            color: [
                                {
                                r: 0,
                                g: 0,
                                b: 0,
                                a: () => Math.sin(getSceneTimeFromStart()*5+i) 
                                }
                            ],
                            position: [
                                {
                                x: () => 0.4 - (0.8 * i),
                                y: () => -0.4,
                                z: () => 0
                                }
                            ]
                        });
                }
    
    this.loader.addAnimation(
    {
        "start": 0, "duration": duration, "layer": layer
        ,"image": "curtain.png",
        color: [
            {
              r: 1.,
              g: 1,
              b: 1
            }
          ],
                  scale: [{ uniform3d: 10 }],
                  
    });
    this.loader.addAnimation({
        start: start,
        duration: duration,
        layer: 500,
        light: {
          type: 'Directional',
          // animate intensity
          properties: { intensity: () => 13 }
        },
        color: [{ r: 1.0, g: 1.0, b: 1.0 }],
        position: [
          {
            x: () => 0,
            y: () => 2,
            z: () => 1
          }
        ]
      });

    this.loader.addAnimation([
        {
          start: start,
          duration: 100,
          layer: 500,
          scale: [{ uniform3d: () => Sync.get("Nakki2") }],
          
          object: { name: 'chikentheather.obj' },
          position: [
            {
              x: () => 0,
              y: () => Math.sin(Math.sin(Sync.get("Nakki")*getSceneTimeFromStart()*0.5)),
              z: () => Math.sin(Math.sin(Sync.get("Nakki1")*getSceneTimeFromStart()*0.5))
            }
          ],
          color: [
            {
              r:  () => 1,
              g:  () => 1,
              b:  () => 1
            }
          ],
          angle: [
            {
              degreesY: () => getSceneTimeFromStart()*150,
              degreesX: () => 0,
              degreesZ: () => 0
            }
          ]
        }
      ]);

      let explotime = 55;
          //savu
  this.addEffectExplosion(
    "bokasd.png",
    null,                   // model
    explotime+.7, 6,  // startTime, duration
    17, 200, 3,  // maxDist, amount, scale
    0, 0, 0,        // posX, posY, posZ
    0, 0, 0,        // startDim
    .3, .3, .25,   // dimX, dimY, dimZ
    0, 0, 0,        // xOffset, yOffset, zOffset
    "NormalBlending", // blendmode
    null);
      //veri
    this.addEffectExplosion(
    "bokfsd.png",
    null,                   // model
    explotime, 6,  // startTime, duration
    15, 150, 1,  // maxDist, amount, scale
    0, 0, 0,        // posX, posY, posZ
    0, 0, 0,        // startDim
    .3, .3, .25,   // dimX, dimY, dimZ
    0, 0, 0,        // xOffset, yOffset, zOffset
    "NormalBlending", // blendmode
    null);

        //höyhen
    this.addEffectExplosion(
    "bokwq.png",
    null,                   // model
    explotime, 6,  // startTime, duration
    15, 175, 0.15,  // maxDist, amount, scale
    0, 0, 0,        // posX, posY, posZ
    0, 0, 0,        // startDim
    .3, .3, .25,   // dimX, dimY, dimZ
    0, 0, 0,        // xOffset, yOffset, zOffset
    "NormalBlending", // blendmode
    null);

        this.addEffectExplosion(
    "BooM.png",
    null,                   // model
    explotime, 6,  // startTime, duration
    15, 25, 0.1,  // maxDist, amount, scale
    0, 0, 0,        // posX, posY, posZ
    0, 0, 0,        // startDim
    .3, .3, .25,   // dimX, dimY, dimZ
    0, 0, 0,        // xOffset, yOffset, zOffset
    "NormalBlending", // blendmode
    null);

        this.addEffectExplosion(
    "EXpode.png",
    null,                   // model
    explotime, 6,  // startTime, duration
    5, 50, 0.5,  // maxDist, amount, scale
    0, 0, 0,        // posX, posY, posZ
    0, 0, 0,        // startDim
    .3, .3, .25,   // dimX, dimY, dimZ
    0, 0, 0,        // xOffset, yOffset, zOffset
    "NormalBlending", // blendmode
    null);



       this.loader.addAnimation(
            {
                "start": 60, "duration": duration, "layer": layer
                ,"image": "unfunnychikengrave.png",
                scale: [{ uniform3d: 2 }],
                color: [
                    {
                      r: 1,
                      g: 1,
                      b: 1
                    }
                  ],

            });


                   this.addEffectExplosion(
    "rain.png",
    null,                   // model
    explotime+5, 25,  // startTime, duration
    5, 200, 0.5,  // maxDist, amount, scale
    0, 6, 0,        // posX, posY, posZ
    4, 12, 0,        // startDim
    0, 0, 0,   // dimX, dimY, dimZ
    0, -10, 0,        // xOffset, yOffset, zOffset
    "NormalBlending", // blendmode
    null);


                    this.addEffectExplosion(
    "rain.png",
    null,                   // model
    explotime+10, 25,  // startTime, duration
    5, 200, 0.5,  // maxDist, amount, scale
    0, 6, 0,        // posX, posY, posZ
    4, 12, 0,        // startDim
    0, 0, 0,   // dimX, dimY, dimZ
    0, -10, 0,        // xOffset, yOffset, zOffset
    "NormalBlending", // blendmode
    null);


                       this.addEffectExplosion(
    "rain.png",
    null,                   // model
    explotime+15, 25,  // startTime, duration
    5, 200, 0.5,  // maxDist, amount, scale
    0, 6, 0,        // posX, posY, posZ
    4, 12, 0,        // startDim
    0, 0, 0,   // dimX, dimY, dimZ
    0, -10, 0,        // xOffset, yOffset, zOffset
    "NormalBlending", // blendmode
    null);



    this.loader.addAnimation(
    {
        "start": 73, "duration": duration, "layer": layer
        ,"image": "_embedded/defaultWhite.png",
        color: [
            {
              r: 0,
              g: 0,
              b: 0
            }
          ],
    });




}


Demo.prototype.addEffectExplosion = function (particleTexture, particleModel, startTime, duration, maxDist, amount, particleScale, posX, posY, posZ, xStartDim, yStartDim, zStartDim, xDim = 0.1, yDim =0.1, zDim = 0.0, xOffset, yOffset, zOffset, blendingMode = "CustomBlending", parentId = "scene")
{

  let particles = new Array(amount);
  for (let i = 0; i < particles.length; i++) {

    let xStart = Math.random() * xStartDim - xStartDim*.5;
    let yStart = Math.random() * yStartDim - yStartDim*.5;
    let zStart = Math.random() * zStartDim - zStartDim*.5;
    let xStartDir = (xStart != 0) ? xStart/Math.abs(xStart) : 0;
    let yStartDir = (yStart != 0) ? yStart/Math.abs(yStart) : 0;
    let zStartDir = (zStart != 0) ? zStart/Math.abs(zStart) : 0;
    let xDir = Math.random() * (xDim+xStartDir);
    let yDir = Math.random() * (yDim+yStartDir);
    let zDir = Math.random() * (zDim+zStartDir);
    xStart+=posX;
    yStart+=posY;
    zStart+=posZ;

    let vectorLength = Math.sqrt(xDir*xDir+yDir*yDir+zDir*zDir);

    xDir = vectorLength*xStartDir*maxDist + (xDir/vectorLength) * (Math.random() * maxDist * 2 - maxDist) + xOffset + xStart;
    yDir = vectorLength*yStartDir*maxDist + (yDir/vectorLength) * (Math.random() * maxDist * 2 - maxDist) + yOffset + yStart;
    zDir = vectorLength*zStartDir*maxDist + (zDir/vectorLength) * (Math.random() * maxDist * 2 - maxDist) + zOffset + zStart;

    
    particles[i] = {
      "x1": xStart,
      "y1": yStart,
      "z1": zStart,
      "x2": xDir,
      "y2": yDir,
      "z2": zDir,
      "startAngle": Math.random() * 360,
      "speedAngle": (Math.random()*4-2)*(250),
    };
  }


  let explosionAnimation = {
    "parent": (parentId==="scene") ? undefined:parentId,
    "start": startTime, "duration": duration,
    "image": particleTexture,
    textureProperties: [{},{minFilter: 'NearestMipmapNearestFilter', magFilter: 'LinearFilter'}],
    "perspective": "3d",
    "billboard": true,
    "scale":[{"uniform3d":1.0}],
    "material":{
      "blending": blendingMode,
      "transparent":true,
      "depthWrite":blendingMode == false
    },
    "instancer": {
      "count": particles.length,
      "runInstanceFunction": (properties) => {

        const i = properties.index;
        //const count = properties.count;
        //const time = properties.time;
        let object = properties.object;
        let color = properties.color;
        let angle = properties.angle;

        const scale = particleScale;
        object.scale.x = scale;
        object.scale.y = scale;
        object.scale.z = scale;
        
        const particle = particles[i];

        const now = getSceneTimeFromStart();

        angle.degreesZ = particle.startAngle+now*(particle.speedAngle);

        const percent = (now-startTime)/duration;

        const mix = Math.log((percent*7.0+1.0))/3;
        object.position.x = Utils.mix(particle.x1, particle.x2, mix);
        object.position.y = Utils.mix(particle.y1, particle.y2, mix);
        object.position.z = Utils.mix(particle.z1, particle.z2, mix);
        color.a = 1.0-percent*percent;
        color.r = color.a;
        color.g = color.a;
        color.b = color.a;
        }
      }
    }

  this.loader.addAnimation(explosionAnimation);
}