/**
 * 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, -30);
        label.text = "Level "+(this.level+1);
        label.color = "white";
        label.textAlign = "center";
        this.addChild(label);

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

            line.context.fillStyle = "black";
            line.context.fillRect(0,4,255,4);
            line.context.fillStyle = "#00caf2";
            line.context.fillRect(0,4,myObj.level*3,4);
        });

        var base_x = bar.x;

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

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

            line.context.fillStyle = "black";
            line.context.fillRect(0,4,255,4);
            line.context.fillStyle = "#00caf2";
            line.context.fillRect(0,4,myObj.level*3,4);
        });
    }
});
