/**
 * @license Licensed under the Apache License, Version 2.0 (the "License"):
 *          http://www.apache.org/licenses/LICENSE-2.0
 */

/**
 * @fileoverview Code generator for the test 2 blocks.
 */
'use strict';

goog.provide('Blockly.Python.music');
goog.require('Blockly.Python');

Blockly.Python['MusicMidiSetup'] = function(block) {
    Blockly.Python.addInclude('SPI', '#include  <SPI.h>');
    Blockly.Python.addInclude('SD', '#include  <SD.h>');
    Blockly.Python.addInclude('MusicPlayer', '#include  <MusicPlayer.h>\n');

    var setup = 'player.beginInMidiFmt();\n' +
        '  player.midiSetInit(' + block.getFieldValue('INSTRUMENT') + ', ' + block.getFieldValue('VOLUME') + ');\n';
    Blockly.Python.addSetup('music_setup', setup);

    var code = '';
    return code;
};

Blockly.Python['MusicSetVolume'] = function(block) {
    var musicVolume = block.getFieldValue('VOLUME');

    var code = 'player.setVolume(' + musicVolume + ');\n';
    return code;
};

Blockly.Python['MusicSetInstrument'] = function(block) {
    var musicInstrument = block.getFieldValue('INSTRUMENT');

    var code = 'player.midiSetInstrument(' + musicInstrument + ');\n';
    return code;
};

Blockly.Python['MusicNote'] = function(block) {
    var musicNote = block.getFieldValue('NOTE');

    var code = musicNote;
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['MusicBeat'] = function(block) {
    var musicBeat = block.getFieldValue('BEAT');

    var code = musicBeat;
    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['MusicPlayTone'] = function(block) {
    var musicNote = Blockly.Python.valueToCode(block, 'NOTE', Blockly.Python.ORDER_ATOMIC);
    var musicDuration = Blockly.Python.valueToCode(block, 'DURATION', Blockly.Python.ORDER_ATOMIC);

    var code = 'player.midiPlay(' + musicNote + ', ' + musicDuration + ');\n';
    return code;
};

Blockly.Python['MusicRest'] = function(block) {
    var musicBeat = Blockly.Python.valueToCode(block, 'BEAT', Blockly.Python.ORDER_ATOMIC);

    var code = 'delay(' + musicBeat + ');\n';
    return code;
};

Blockly.Python['Music_mp3_setup'] = function(block) {
    Blockly.Python.addInclude('SPI', '#include  <SPI.h>');
    Blockly.Python.addInclude('SD', '#include  <SD.h>');
    // Blockly.Python.addInclude('arduino', '#include <arduino.h>');
    Blockly.Python.addInclude('MusicPlayer', '#include  <MusicPlayer.h>\n');
    var vol = block.getFieldValue("SETVOL") || 50;
    Blockly.Python.addDeclaration("mp3_dec","int _musicPlayerVolume = "+ vol+ ";\n"+
        "int _musicVolumeMappingRead, _musicVolumeMappingSet;\n")

    Blockly.Python.addSetup("mp3_setup", " Serial.begin(9600);\n" +
        "  delay(2000);\n" +
        "  player.keyDisable(); //keys disable first;\n" +
        // "  player.digitalControlEnable();//enable to scan the 0-7\n" +
        // "  player.analogControlEnable(); //enable to scan the A4/A5\n" +
        "  player.begin(); //will initialize the hardware and set default mode to be normal.\n" +
        "  player.setVolume(_musicPlayerVolume);\n");

    Blockly.Python.addFunction("func_musicPlaySong", "/*\n"+ 
        "*  Music Function: Music Set Play Song\n"+
        "*/\n"+
        "void _musicPlaySong(String songname) {\n"+
        "  int songNameLength = songname.length() + 1;\n"+
        "  char songNameBuf[songNameLength];\n"+
        "  songname.toCharArray(songNameBuf, songNameLength);\n"+
        "\n"+
        "  delay(50);\n"+
        "  player.opPause();\n"+
        "  player.setPlayMode(PM_REPEAT_ONE);\n"+
        "  player.playOne(songNameBuf);\n"+
        "  player.play();\n"+
        "  delay(50);\n"+
        "}")
    
    Blockly.Python.addFunction("func__musicOperation", "/* \n"+
        " *  Music Function: Music Operation\n"+
        " */\n"+
        "\n"+
        "void _musicOperation(int operation) {\n"+
        "  _musicVolumeMappingRead = map(_musicPlayerVolume, 110, 10, 0, 10);\n"+
        "\n"+
        "  if (operation == 0) {          // 0: Play song\n"+
        "    delay(100);\n"+
        "    player.opResume();\n"+
        "  } else if (operation == 1) {   // 1: Pause song\n"+
        "    delay(100);\n"+
        "    player.opPause();\n"+
        "  } else if (operation == 2) {   // 2: Previous song\n"+
        "    delay(100);\n"+
        "    for (int a = 0; a < 1; a++) {\n"+
        "      player.opPreviousSong();\n"+
        "      player.play();\n"+
        "    }\n"+
        "  } else if (operation == 3) {   // 3: Next song\n"+
        "    delay(100);\n"+
        "    for (int a = 0; a < 1; a++) {\n"+
        "      player.opNextSong();\n"+
        "      player.play();\n"+
        "    }\n"+
        "  } else if (operation == 4) {   // 4: Volume Up\n"+
        "\n"+
        "    _musicPlayerVolume = _musicPlayerVolume - 10;\n"+
        "    if ( _musicPlayerVolume <= 10 ) {\n"+
        "      _musicPlayerVolume = 10;\n"+
        "    }\n"+
        "    delay(100);\n"+
        "    player.setVolume(_musicPlayerVolume);\n"+
        "  } else if (operation == 5) {   // 5: Volume Down\n"+
        "\n"+
        "    _musicPlayerVolume = _musicPlayerVolume + 10;\n"+
        "    if ( _musicPlayerVolume >= 110 ) {\n"+
        "      _musicPlayerVolume = 110;\n"+
        "    }\n"+
        "    delay(100);\n"+
        "    player.setVolume(_musicPlayerVolume);\n"+
        "  } else if (operation == 6) {   // 6: Play all song\n"+
        "    delay(100);\n"+
        "    player.setPlayMode(PM_REPEAT_LIST);\n"+
        "    player.scanAndPlayAll();\n"+
        "    player.play();\n"+
        "    delay(100);\n"+
        "    player.play();\n"+
        "  }\n"+
        "}\n")

    Blockly.Python.addFunction("func_musicSetVolume","/* \n"+
        " *  Music Function: Music Set Volume\n"+
        " */\n"+
        "\n"+
        "void _musicSetVolume(int volume) {\n"+
        "  _musicVolumeMappingSet = map(volume, 0, 10, 110, 10);\n"+
        "  player.setVolume(_musicVolumeMappingSet);\n"+
        "\n"+
        "  _musicPlayerVolume = _musicVolumeMappingSet;\n"+
        "}\n")
    return "";
}



Blockly.Python['Music_mp3_playOneWithName'] = function(block) {
    var music_name = block.getFieldValue("MUSIC_NAME");
    var code = "_musicPlaySong(\"" + music_name + "\");\n";
    return code;
}

Blockly.Python['Music_mp3_playAllMusic'] = function(block) {
    // var type = block.getFieldValue("TYPE");
    Blockly.Python.addSetup("playMode", "player.setPlayMode(PM_NORMAL_PLAY);\n ");
    var code = "player.scanAndPlayAll();\n" +
        "player.play();\n";
    return code;
}

Blockly.Python['Music_mp3_playSetting'] = function(block) {
    var setting = block.getFieldValue("SETTING");
    // switch (setting) {
    //     case "pause":
    //         code = "player.opPause();\n";
    //         break;
    //     case "resume":
    //         code = "player.opResume();\n";
    //         break;
    //     case "stop":
    //         code = "player.opStop();\n";
    //         break;
    //     case "next":
    //         code = "player.opNextSong();\n";
    //         break;
    //     case "pre":
    //         code = "player.opPreviousSong();\n";
    //         break;
    //     default:
    //         break;
    // }
    var code = "_musicOperation("+setting+");\n"
    return code;
}

Blockly.Python['Music_mp3_getCurrentVolume'] = function(block) {
    var code = "_musicVolumeMappingRead";
    return [code, Blockly.Python.ORDER_ATOMIC];
}

Blockly.Python['Music_mp3_setCurrentVolume'] = function(block) {
    var vol_level = block.getFieldValue("VOLLEVEL")
    var code = "_musicSetVolume("+vol_level+");\n";
    return code; 
}