/**
 * Created with JetBrains WebStorm.
 * User: gravis_ihara
 * Date: 14/09/17
 * Time: 19:13
 * To change this template use File | Settings | File Templates.
 */
gravis.SliderScene = enchant.Class.create(enchant.widget.ConfirmScene, {
    initialize: function(content, acceptName, cancelName) {
        enchant.widget.ConfirmScene.call(this, content, acceptName, cancelName);

        var myObj = this;
        this.level = gravis.Utility.savedata.cpu;

        var label = new enchant.Label();
        label.alignHorizontalCenterIn(this).alignVerticalCenterIn(this);
        label.moveBy(0, -20);
        label.text = "Level "+(this.level+1);
        label.color = "white";
        label.textAlign = "center";
        label.font = "12px sans-serif";
        this.addChild(label);

        var line = new enchant.Surface(127,6);
        line.context.beginPath();
        line.context.fillStyle = "black";
        line.context.fillRect(0,2,127,2);
        line.context.fillStyle = "#00caf2";
        line.context.fillRect(0,2,this.level*1.5,2);
        var bar = new enchant.Sprite(127,6);
        bar.image = line;
        bar.alignHorizontalCenterIn(this).alignVerticalCenterIn(this);
        this.addChild(bar);
        bar.addEventListener('touchstart', function(e){
            button.x = e.x - 5;
            if( button.x < base_x - 5 ) button.x = base_x - 5;
            if( button.x > base_x - 5 + 127 ) button.x = base_x - 5 + 127;
            myObj.level = Math.floor((button.x - (base_x - 5)) / 1.5);
            label.text = "Level "+(myObj.level+1);

            line.context.fillStyle = "black";
            line.context.fillRect(0,2,127,2);
            line.context.fillStyle = "#00caf2";
            line.context.fillRect(0,2,myObj.level*1.5,2);
        });

        var base_x = bar.x;

        var circle = new enchant.Surface(10,10);
        circle.context.beginPath();
        circle.context.fillStyle = "white";
        circle.context.arc(5,5,5,0,Math.PI*2,false);
        circle.context.fill();
        circle.context.beginPath();
        circle.context.fillStyle = "black";
        circle.context.lineWidth = 2;
        circle.context.arc(5,5,4.5,0,Math.PI*2,false);
        circle.context.stroke();
        var button = new enchant.Sprite(10,10);
        button.image = circle;
        button.alignHorizontalCenterIn(this).alignVerticalCenterIn(this);
        button.x = base_x + Math.floor(this.level * 1.5) - 5;
        if( button.x > base_x - 5 + 127 ) button.x = base_x - 5 + 127;
        this.addChild(button);

        button.addEventListener('touchmove', function(e){
            button.x = e.x - 5;
            if( button.x < base_x - 5 ) button.x = base_x - 5;
            if( button.x > base_x - 5 + 127 ) button.x = base_x - 5 + 127;
            myObj.level = Math.floor((button.x - (base_x - 5)) / 1.5);
            label.text = "Level "+(myObj.level+1);

            line.context.fillStyle = "black";
            line.context.fillRect(0,2,127,2);
            line.context.fillStyle = "#00caf2";
            line.context.fillRect(0,2,myObj.level*1.5,2);
        });
    }
});
