/* --------------------------------------------------- */
/* Functions                                           */
/* --------------------------------------------------- */
let coview_api = new CoviewApi();


$(function() {
    var coviewApiActive = coview_api.Init (
        {
            testSTRParam : "param1",
            testNUMParam : 77,
            testBOOLParam : false,
            coview_api_srv_addr: "https://app.coview.co.kr:7443",
            coview_wrap_id : "coviewShare",
        }
    );

    // special button IN Chat ui



    $("#coviewShareMainBtn").on("click", function(e) {
        e.stopPropagation();
        if($(".coview_share_dropdown_bar").is(':visible') == true) {
            Coview_shareDropdownHide();

            if($(".coview_share_photo_select_bubble").is(':visible') == true) {
                $(".coview_share_photo_select_bubble").slideUp();
            }
        } else {
            $(".coview_share_dropdown_bar").slideDown();
        }
    });

    // ナビゲーションバー
    // 協業タイプ変更ボタン
    $("#coviewChangeTypeButton").on("click", function(e) {
        // ナビゲーションボタン
        $("#coviewChangeTypeButton").hide();
        $("#coviewAddMemberButton").hide();
        $("#coviewMemberButton").hide();
        $("#coviewShareCloseButton").show();
        // 協業タイプ選択画面表示
        $(".coview_type_area").show();
    });

    // 写真共有
    $("#photoBtn").on("click", function(e) {
        console.log("写真共有");
        // TODO
    });

    // 現場中継
    $("#liveBtn").on("click", function(e) {
        console.log("現場中継");
        // TODO
    });

    // 文書共有
    $("#documentBtn").on("click", function(e) {
        console.log("文書共有");
        // TODO
    });

    // 落書き
    $("#boadBtn").on("click", function(e) {
        console.log("落書き");
        // TODO
    });

    // 音声共有
    $("#voiceBtn").on("click", function(e) {
        console.log("音声共有");
        // TODO
    });

    // メンバー招待ボタン
    $("#coviewAddMemberButton").on("click", function(e) {
	    // ナビゲーションボタン
        $("#coviewChangeTypeButton").hide();
        $("#coviewAddMemberButton").hide();
        $("#coviewMemberButton").hide();
        $("#coviewShareCloseButton").show();
        // メンバー招待画面表示
        $(".coview_add_member_area").show();
    });

    // メンバー表示ボタン
    $("#coviewMemberButton").on("click", function(e) {
        // ナビゲーションボタン
        $("#coviewChangeTypeButton").hide();
        $("#coviewAddMemberButton").hide();
        $("#coviewMemberButton").hide();
        $("#coviewShareCloseButton").show();
        // メンバー表示
        coview_api.GetMemberList();
        $(".coview_member_area").show();
    });

    // 閉じるボタン
    $("#coviewShareCloseButton").on("click", function(e) {
        // ナビゲーションボタン
        $("#coviewChangeTypeButton").show();
        $("#coviewAddMemberButton").show();
        $("#coviewMemberButton").show();
        $("#coviewShareCloseButton").hide();
        // 画面表示
        $(".coview_type_area").hide();
        $(".coview_add_member_area").hide();
        $(".coview_member_area").hide();
    });


    // マイクボタン
    $("#coviewMicBtn").on("click", function(e) {
        var flg = $(this).hasClass("active");
        if (flg == true) {
            $(this).removeClass("active");
            document.getElementById('coviewMicBtn').innerHTML = '<img class="" src="./icon/ic_collaboration_mic_off.png" alt=""> ';
            // マイクOFF
            coview_api.micMuteControl("off");
        } else {
            $(this).addClass("active");
            document.getElementById('coviewMicBtn').innerHTML = '<img class="" src="./icon/ic_collaboration_mic_on.png" alt=""> ';
            // マイクON
            coview_api.micMuteControl("on");
        }
    });

    // レコードボタン
    $("#coviewRecBtn").on("click", function(e) {
        var flg = $(this).hasClass("active");
        if (flg == true) {
            $(this).removeClass("active");
            document.getElementById('coviewRecBtn').innerHTML = '<img class="" src="./icon/ic_collaboration_record.png" alt="">';
            // 記録終了

        } else {
            $(this).addClass("active");
            document.getElementById('coviewRecBtn').innerHTML = '<img class="" src="https://app.coview.co.kr:7443/office_img/api/icon_api_record_stop.png" alt="">';
            // 記録開始

        }
    });

    // 退室ボタン
    $("#coviewLeaveRoomBtn").on("click", function(e) {
        coview_api.LeaveRoom();
        $(".coview_share_area").hide();
    });


    $("body").on("click", function() {
        Coview_shareDropdownHide();
    });

    $(document).on("click", ".coview_share_request", function() {
        console.log("globalUserInfo", globalUserInfo);
        if($(this).hasClass("video") == true) {
            globalUserInfo.coWorkType = "video";
            $(".coview_share_title_name").text("LIVE");
        } else {
            globalUserInfo.coWorkType = "image";
            $(".coview_share_title_name").text("IMAGE");
        }
        coview_api.JoinRoom(globalUserInfo.roomId, globalUserInfo.coWorkType);
        Coview_moveToVideoShareArea();
    });

    // $('#coviewShareRequestVideo').bind('click', function()
    //     console.log("globalUserInfo", globalUserInfo);
    //     globalUserInfo.coWorkType = "video";
    //     coview_api.JoinRoom(globalUserInfo.roomId, globalUserInfo.coWorkType);
    // });

    /* --------------------------------------------------- */
    /* 協業タブ      　　　　　                               */
    /* --------------------------------------------------- */

    // 通話
    $("#coviewShareChat").on("click", function() {
        console.log("ON click coviewShareChat");
        // 協業画面へ遷移
        globalUserInfo.coWorkType = "audio";
        coview_api.CreateRoom(globalUserInfo.roomId, globalUserInfo.coWorkType);
        Coview_moveToVideoShareArea();
    });

    // 文書
    $("#coviewShareDoc").on("click", function() {
        console.log("ON click coviewShareDoc");
        alert("INTO a document share mode (only audio)");
        globalUserInfo.coWorkType = "audio";
    });

    // 画像()
    $("#coviewSharePhoto").on("click", function(e) {
        e.stopPropagation();
        console.log("ON click coviewSharePhoto");
        if($(".coview_share_photo_select_bubble").is(':visible') == true) {
            $(".coview_share_photo_select_bubble").slideUp();
        } else {
            $(".coview_share_photo_select_bubble").slideDown();
        }
    });

    // 画像(カメラ)
    $("#coviewSharePhotoCamera").on("click", function() {
        $(".coview_share_photo_select_bubble").hide();
        globalUserInfo.coWorkType = "camera";
        coview_api.CreateRoom(globalUserInfo.roomId, globalUserInfo.coWorkType);
        Coview_moveToVideoShareArea();

    });

    // 画像(ライブラリ)
    $("#coviewSharePhotoGalary").on("click", function() {
        $(".coview_share_photo_select_bubble").hide();
        globalUserInfo.coWorkType = "galary";
        coview_api.CreateRoom(globalUserInfo.roomId, globalUserInfo.coWorkType);
        Coview_moveToVideoShareArea();
    });

    // 動画
    $("#coviewShareLive").on("click", function() {
        console.log("ON click coviewShareLive");
        globalUserInfo.coWorkType = "video";
        console.log("globalUserInfo", globalUserInfo);

        // var chars = "0123456789";
        // var string_length = 6;
        // var randomstring = '';
        // for (var i=0; i<string_length; i++) {
        //     var rnum = Math.floor(Math.random() * chars.length);
        //     randomstring += chars.substring(rnum,rnum+1);
        // }
        // coview_api.CreateRoom(randomstring, globalUserInfo.coWorkType);

        coview_api.CreateRoom(globalUserInfo.roomId, globalUserInfo.coWorkType);
        Coview_moveToVideoShareArea();
    });

    // ボード
    $("#coviewShareBoad").on("click", function() {
        console.log("ON click coviewShareBoad");
        // 協業画面へ遷移
        globalUserInfo.coWorkType = "audio";
        coview_api.CreateRoom(globalUserInfo.roomId, globalUserInfo.coWorkType);
        Coview_moveToVideoShareArea();
    });


    coview_api.addEventListener ("ready", function () {
        console.log("=============> READY : ready for coview api");
        coview_api.Login(globalUserInfo.loginId);
    });

    coview_api.addEventListener ("start", function () {
        console.log("=============> START : share start");
        $("#loadingIndicator").removeClass("full_active");
    });

    coview_api.addEventListener ("destroy", function () {
        console.log("=============> START : share start");
        coview_api.LeaveRoom();
        $(".coview_share_area").hide();
        $("#loadingIndicator").removeClass("full_active");
    });

    coview_api.addEventListener ("message", async function (event, json) {
        console.log("+++ addEventListener", json);
        console.log("processMessage api = ", json.api);

        switch (json.api){
            case "LoginResponse":
            break;
            case "CreateRoomResponse":
                if(json.resultCode == 200){
                    var coviewInviteMessage = "coviewsharerequest:" + globalUserInfo.coWorkType;
                    socket.emit('createMessage', {
                      text: coviewInviteMessage
                    }, 0);
                } else {
                }
            break;

            case "joinRoomInfoResponse":
                if(json.resultCode == 200){
                } else {
                    // alert(json.resultMsg + "(" + json.resultCode + ")" );
                    alert("Not exist sharing call (" + json.resultCode + ")\nPlease using share menu.");
                    coview_api.LeaveRoom();
                    $(".coview_share_area").hide();
                    $("#loadingIndicator").removeClass("full_active");
                }
                break;

            default:
                // console.log('Unrecognized api', json);
                break;
        }
    });
});

function Coview_shareDropdownHide() {
    $(".coview_share_dropdown_bar").slideUp();
    $(".coview_share_photo_select_bubble").slideUp();
}

// 協業画面へ遷移
function Coview_moveToVideoShareArea() {
    $("#loadingIndicator").addClass("full_active");
    $(".coview_share_area").show();
    $(".coview_type_area").hide();
    $(".coview_add_member_area").hide();
    $(".coview_member_area").hide();
    $(".coview_share_title_name").text(getLocalizedString("talking"));
    // ナビゲーションバーの表示
    $("#coviewChangeTypeButton").show();
    $("#coviewAddMemberButton").show();
    $("#coviewMemberButton").show();
    $("#coviewShareCloseButton").hide();
    // 設定
    Coview_setCoviewTypeBtn();
}

// 協業タイプ選択画面の設定
function Coview_setCoviewTypeBtn() {
    // 様式を読み込む
    const typeTemplate = $('#coview-type-template').html();

    switch (globalUserInfo.coWorkType) {
        case "audio":
            let html = Mustache.render(typeTemplate, {
                btnId1: "photoBtn",
                imgUrl1: "./icon/ic_collaboration_record.png",
                type1: getLocalizedString("sharePhoto"),
                btnId2: "liveBtn",
                imgUrl2: "./icon/ic_collaboration_record.png",
                type2: getLocalizedString("shareLive"),
                btnId3: "documentBtn",
                imgUrl3: "./icon/ic_collaboration_record.png",
                type3: getLocalizedString("shareDocument"),
                btnId4: "boadBtn",
                imgUrl4: "./icon/ic_collaboration_record.png",
                type4: getLocalizedString("shareBoad")
            });
            let obj = $(jQuery.parseHTML(html))
            $('#coviewTypeArea').append(obj);
            break;
        // TODO　他のケース
        default:
    }
}

