// 中间黑色边框和里面的内容（录音状态）
var blackBoxSpeak = document.querySelector('.blackBoxSpeak');
blackBoxSpeak.style.background = 'url("../image/audio/ic_record@2x.png")no-repeat 28 16px/65px 104px, url("../image/audio/ic_record_ripple@2x-9.png")no-repeat 111.2px 32px/28.8px 88px';
blackBoxSpeak.style.backgroundColor = 'rgba(0,0,0,.7)';

// 轮播相关
var index = [9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9];

var num = index.length;

var timer = null; // 用于清除计时器

var timeInterval = null;

function initEvent(i, host, access_token, timestamp, nonce, sign, audioStreamer) {
    var dom = document.getElementById('bt_recoding' + i);

    var soundinput = document.getElementById('sound' + i);

    var icon = document.getElementById('icon' + i);

    var delsound = document.getElementById('delsound' + i);

    var time;

    dom.addEventListener('touchstart', function (event) {
        if (soundinput.value) {
            // 播放
            audioStreamer.stop();

            var soundpath = soundinput.value.split('@=%=@')[1];

            audioStreamer.openPlayer({
                path: host + '/' + soundpath
            }, function (ret) {
                console.log(JSON.stringify(ret));

                if (ret.status) {
                    console.log(ret.duration);
                } else {
                    customToast(lang('5555528'), 2000);
                }
            });
        } else {
            setTimer();

            time = 0;

            timeInterval = setInterval(function () {
                time++;
            }, 1000);

            if (event.cancelable) {
                event.preventDefault(); // 阻止浏览器默认行为
            }

            // 显示录音
            dom.value = '松开结束';
            blackBoxSpeak.style.display = 'block';

            // 开始录音
            api.startRecord();
        }
    });

    dom.addEventListener('touchmove', function (event) {
        if (soundinput.value) {

        } else {
            if (event.cancelable) {
                event.preventDefault(); // 阻止浏览器默认行为
            }

            // 显示录音
            dom.value = '松开结束';
            blackBoxSpeak.style.display = 'block';
        }
    });

    dom.addEventListener('touchend', function (event) {
        if (soundinput.value) {

        } else {
            if (event.cancelable) {
                event.preventDefault(); // 阻止浏览器默认行为
            }

            clearInterval(timer);

            clearInterval(timeInterval);

            // 初始化状态
            dom.value = '按住说话';
            blackBoxSpeak.style.display = 'none';

            api.stopRecord(function (ret, err) {
                if (ret) {
                    console.log(time);

                    if (time <= 30) {
                        // 上传音频文件
                        api.ajax({
                            url: host + '/Config/Upload/UploadImage',
                            method: 'post',
                            timeout: 300,
                            headers: {
                                token: access_token,
                                timestamp: timestamp,
                                nonce: nonce,
                                sign: sign
                            },
                            data: {
                                files: {
                                    file: ret.path
                                }
                            }
                        }, function (ret, err) {
                            if (ret) {
                                console.log(JSON.stringify(ret));

                                if (ret.ret == 1) {
                                    dom.value = time + 's';

                                    soundinput.value = time + '@=%=@' + ret.data;

                                    icon.style.display = 'block';
                                } else {
                                    customToast(lang(ret.msg), 2000);
                                }
                            } else {
                                console.log(JSON.stringify(err));

                                customToast(lang('5555538'), 2000);
                            }
                        });
                    } else {
                        customToast(lang('5555530'), 2000);
                    }
                } else {
                    console.log(JSON.stringify(err));

                    customToast(lang('5555527'), 2000);
                }
            });
        }
    });

    delsound.addEventListener('click', function () {
        dom.value = '按住说话';

        soundinput.value = '';

        icon.style.display = 'none';

        audioStreamer.stop();
    });
}

// 轮播
function setTimer() {
    timer = setInterval(function () {
        setTimeout(function () {
            num++;

            blackBoxSpeak.style.background = 'url("../image/audio/ic_record@2x.png")no-repeat 28px 16px/64px 104px, url("../image/audio/ic_record_ripple@2x-' + index[num] + '.png")no-repeat 111.2px 32px/28.8px 88px';

            blackBoxSpeak.style.backgroundColor = 'rgba(0,0,0,.7)';
        }, 80);

        if (num >= index.length - 1) {
            num = 0;
        }
    }, 80);
}