<button type='button' id='{{AUTOID}}' data-currentsentence="0" class="roundmini-audio-player" >
    <audio id='{{AUTOID}}_player' class="roundminiaudio nomediaplugin">
    </audio>
    <i class="fa fa-volume-up" aria-hidden="true"></i>
</button>

{{#js}}
 require(['jquery','core/log', 'filter_poodll/pollyhelper'], function($, log, pollyhelper) {

    var wordselector = '#{{AUTOID}}_textblock span.tbr_word';
    var sentenceselector = '#{{AUTOID}}_textblock span.tbr_sentence';
     var passagecssclass = 'filterpoodll_pr_polly_cont';

    var pr = {
        voice: 'Amy',
        usetext: '',
        sentences: [],
        thesentence_number: 0,
        lettered: false,
        theplayer: false,
        textblock:  [],
        itemid: '',
        fa: [],
        pollyhelper: false,

        init: function() {
            // Moodle seems to run the JS twice so we just ignore it if its the second run (otherwise the event handlers register twice etc).
            this.theplayer = $('#{{AUTOID}}_player');
            if (this.theplayer.attr('data-init')==='done'){
                return;
            } else {
                //get the DOM elements we need to work with
                this.itemid = "{{AUTOID}}";
                this.textblock = $('#{{AUTOID}}_textblock');
                this.fa = $('#{{AUTOID}} .fa');
                this.theplayer.attr('data-init','done');

                //set up passage reader specific stuff
                this.passage_reader();

                //set up our event handlers
                this.register_events();
                //set up Polly URLs, sentence naviagation and highlighting on the passage
                //we clone pollyhelper so that multiple instances of the reader can run at once
                //AMD modules share the instances they declare in the module, so we need to clone
                //or they would all play the passage for the finally declared reader instance
                this.pollyhelper = pollyhelper.clone();
                this.pollyhelper.init(this.theplayer,this.itemid, this.textblock,
                            this.voice,sentenceselector,wordselector,
                            passagecssclass,'{{highlightmode}}', '{{CLOUDPOODLLTOKEN}}','{{AWSREGION}}','{{CLOUDPOODLLURL}}');
                this.sentences = $(sentenceselector);
            }
        },


        passage_reader: function(){

            // Fetch the text to read.
            this.usetext = this.textblock.text();

            // If its right to left we need to massage it a bit.
            switch("{{language}}"){
                case 'Arabic':
                case 'Hebrew':
                case 'Farsi':
                    this.textblock.addClass('filter_poodll_passageplayer_rtl');
                    $(this.textblock).find('p').removeAttr('dir');
            }

            // Determine the voice.
            var mf = "{{speaker}}";
            switch("{{language}}"){
                case "English(US)": this.voice = mf=='Male'?'Joey':'Kendra';break;
                case "English(GB)": this.voice = mf=='Male'?'Brian':'Amy';break;
                case "English(AU)": this.voice = mf=='Male'?'Russell':'Nicole';break;
                case "English(IN)": this.voice = mf=='Male'?'Aditi':'Raveena';break;
                case "English(WELSH)": this.voice = mf=='Male'? 'Geraint':'Geraint';break;
                case "Arabic": this.voice = mf=='Male'?'ar-XA-Wavenet-B':'Zeina';break;
                case "Bulgarian": this.voice = mf=='Male'? 'bg-BG-Standard-A':'bg-BG-Standard-A';break;
                case "Czech": this.voice = mf=='Male'? 'cs-CZ-Standard-A':'cs-CZ-Wavenet-A';break;
                case "Danish": this.voice = mf=='Male'?'Mads':'Naja';break;
                case "Dutch": this.voice = mf=='Male'?'Ruben':'Lotte';break;
                case "Dutch(BE)": this.voice = mf=='Male'?'nl-BE-Wavenet-B':'nl-BE-Wavenet-A';break;
                case "French(FR)": this.voice = mf=='Male'?'Mathieu':'Celine';break;
                case "French(CA)": this.voice = mf=='Male'?'Chantal':'Chantal';break;
                case "German": this.voice = mf=='Male'?'Hans':'Marlene';break;
                case "Greek": this.voice = mf=='Male'? 'el-GR-Wavenet-A':'el-GR-Standard-A';break;
                case "Hebrew": this.voice = mf=='Male'?'he-IL-Wavenet-B':'he-IL-Wavenet-A';break;
                case "Icelandic": this.voice = mf=='Male'?'Karl':'Dora';break;
                case "Italian": this.voice = mf=='Male'?'Carla':'Giorgio';break;
                case "Japanese": this.voice = mf=='Male'?'Takumi':'Mizuki';break;
                case "Korean": this.voice = mf=='Male'?'Seoyan':'Seoyan';break;
                case "Latvian": this.voice = mf=='Male'? 'lv-LV-Standard-A':'lv-LV-Standard-A';break;
                case "Lithuanian": this.voice = mf=='Male'? 'lt-LT-Standard-A':'lt-LT-Standard-A';break;
                case "Norwegian": this.voice = mf=='Male'?'Liv':'Liv';break;
                case "Polish": this.voice = mf=='Male'?'Jacek':'Ewa';break;
                case "Portugese(BR)": this.voice = mf=='Male'?'Ricardo':'Vitoria';break;
                case "Portugese(PT)": this.voice = mf=='Male'?'Cristiano':'Ines';break;
                case "Romanian": this.voice = mf=='Male'?'Carmen':'Carmen';break;
                case "Russian": this.voice = mf=='Male'?'Maxim':'Tatyana';break;
                case "Serbian": this.voice = mf=='Male'? 'sr-RS-Standard-A':'sr-RS-Standard-A';break;
                case "Slovak": this.voice = mf=='Male'? 'sk-SK-Wavenet-A':'sk-SK-Standard-A';break;
                case "Spanish(ES)": this.voice = mf=='Male'?'Enrique':'Conchita';break;
                case "Spanish(US)": this.voice = mf=='Male'?'Miguel':'Penelope';break;
                case "Swedish": this.voice = mf=='Male'?'Astrid':'Astrid';break;
                case "Turkish": this.voice = mf=='Male'?'Filiz':'Filiz';break;
                case "Welsh": this.voice = mf=='Male'?'Gwyneth':'Gwyneth';break;
                default: this.voice = mf=='Male'?'Brian':'Amy';
            }

        },

        register_events: function() {
            var that = this;
            /*
            *
            * AUDIO PLAYER events.
            *
            */
            that.theplayer[0].addEventListener('ended', function () {
                if (that.thesentence_number < that.sentences.length - 1) {
                    that.thesentence_number++;
                    that.pollyhelper.doplayaudio(that.thesentence_number);
                } else {
                    that.pollyhelper.dehighlight_all();
                    $(that.fa).removeClass('fa-stop');
                    $(that.fa).addClass('fa-volume-up');
                    that.thesentence_number = 0;
                    that.theplayer.removeAttr('src');
                }
            });

            // Handle audio player button clicks.
            $('#' + that.itemid).click(function () {
                if (!that.theplayer[0].paused && !that.theplayer[0].ended) {
                    that.theplayer[0].pause();
                    if ("{{stoporpause}}" == 'stop') {
                        that.thesentence_number = 0;
                    }
                    $(that.fa).removeClass('fa-stop');
                    $(that.fa).addClass('fa-volume-up');

                    // If paused and not in limbo no src state.
                } else if (that.theplayer[0].paused && that.theplayer.attr('src')) {
                    that.pollyhelper.doplayaudio(that.thesentence_number);
                    $(that.fa).removeClass('fa-volume-up');
                    $(that.fa).addClass('fa-stop');
                    // Play.
                } else {
                    log.debug("stoporpause: " + "{{stoporpause}}");
                    if ("{{stoporpause}}" == 'stop') {
                        that.thesentence_number = 0;
                    }
                    that.pollyhelper.doplayaudio(that.thesentence_number);
                    $(that.fa).removeClass('fa-volume-up');
                    $(that.fa).addClass('fa-stop');
                } // End of if paused ended.
            });

            // Handle sentence clicks.
            $('#' + that.itemid + '_textblock  .tbr_innerdiv').on('click', '.tbr_sentence', function () {
                that.theplayer[0].pause();
                var sentenceindex = $(this).attr('data-sentenceindex');
                $(that.fa).removeClass('fa-volume-up');
                $(that.fa).addClass('fa-stop');
                that.thesentence_number = sentenceindex;
                that.pollyhelper.doplayaudio(sentenceindex);
            });
        }//end of register events
    };
    pr.init();
});
{{/js}}