$(document).ready(function () {
    tableCard();
    $("#dbConnectTest").off('click').on('click', function (e) {
        // $("#cardHeader").off('click').on('click', function(e) {
        //     $("#cardBody").css("display", "");
        // });
        var dbConnectVO = {};
        dbConnectVO["url"] = $("#dbUrl").val();
        dbConnectVO["port"] = $("#dbPort").val();
        dbConnectVO["dbName"] = $("#dbName").val();
        dbConnectVO["userName"] = $("#dbUserName").val();
        dbConnectVO["passWd"] = $("#dbPassWd").val();
        dbConnectVO["connMax"] = 10;

        url = contextPath + '/api/d2v/dbConnect';
        $.ajax({
            url: url,
            type: "POST",
            data: JSON.stringify(dbConnectVO),
            contentType : 'application/json',
            async: true,
            dataType: 'json',
            success: function (result) {
                if(result) {
                    alert("연결성공");
                }
                else {
                    alert("연결실패");
                }
            }
        });
    });
});

function tableCard() {
    $("#tableCard").remove();

    var d2vCard = $("#cardBody");

    var div_card = $("<div/>").addClass("col-7 row").attr("id", "tableCard");
    d2vCard.append(div_card);

    // var div_card = $("<div/>").addClass("card").attr("id", "tableCard");
    // d2vCard.append(div_card);

    // var div_card_header = $("<div/>").addClass("card-header col-7").text("테이블 정보 입력");
    // div_card.append(div_card_header);

    // var div_card_body = $("<div/>").addClass("card-body col-7 row");
    // div_card.append(div_card_body);

    var form_group_11 = $("<div/>").addClass("form-group");
    div_card.append(form_group_11);

    var label_11 = $("<label/>").addClass("form-label").text("jUrl");
    form_group_11.append(label_11);

    var input_tag_11 = $("<input/>").addClass("form-control").attr({
        "type":"text",
        "id":"jUrl",
        "name":"example-text-input",
        "placeholder":"jUrl.."
    });
    form_group_11.append(input_tag_11);

    var form_group_1 = $("<div/>").addClass("form-group");
    div_card.append(form_group_1);

    var label_1 = $("<label/>").addClass("form-label").text("도메인 명");
    form_group_1.append(label_1);

    var input_tag_1 = $("<input/>").addClass("form-control").attr({
        "type":"text",
        "id":"domainTableName",
        "name":"example-text-input",
        "placeholder":"도메인.."
    });
    form_group_1.append(input_tag_1);

    var form_group_2 = $("<div/>").addClass("form-group");
    div_card.append(form_group_2);

    var label_2 = $("<label/>").addClass("form-label").text("도메인 테이블 ID컬럼명");
    form_group_2.append(label_2);

    var input_tag_2 = $("<input/>").addClass("form-control").attr({
        "type":"text",
        "id":"domainIdColumn",
        "name":"example-text-input",
        "placeholder":"도메인 테이블 ID컬럼.."
    });
    form_group_2.append(input_tag_2);

    var form_group_2_2 = $("<div/>").addClass("form-group");
    div_card.append(form_group_2_2);

    var label_2_2 = $("<label/>").addClass("form-label").text("도메인 테이블 제목컬럼명");
    form_group_2_2.append(label_2_2);

    var input_tag_2_2 = $("<input/>").addClass("form-control").attr({
        "type":"text",
        "id":"domainTitleColumn",
        "name":"example-text-input",
        "placeholder":"도메인 테이블 제목컬럼.."
    });
    form_group_2_2.append(input_tag_2_2);

    var form_group_3 = $("<div/>").addClass("form-group");
    div_card.append(form_group_3);

    var label_3 = $("<label/>").addClass("form-label").text("도메인 학습컬럼명");
    form_group_3.append(label_3);

    var input_tag_3 = $("<input/>").addClass("form-control").attr({
        "type":"text",
        "id":"domainTrainingColumn",
        "name":"example-text-input",
        "placeholder":"도메인 학습컬럼.."
    });
    form_group_3.append(input_tag_3);

    var form_group_4 = $("<div/>").addClass("form-group");
    div_card.append(form_group_4);

    var label_4 = $("<label/>").addClass("form-label").text("타겟 도메인명(컬럼 구분자' , ' & 띄어쓰기 없이 작성)");
    form_group_4.append(label_4);

    var input_tag_4 = $("<input/>").addClass("form-control").attr({
        "type":"text",
        "id":"targetTableName",
        "name":"example-text-input",
        "placeholder":"직업,훈련...."
    });
    form_group_4.append(input_tag_4);

    var form_group_5 = $("<div/>").addClass("form-group");
    div_card.append(form_group_5);

    var label_5 = $("<label/>").addClass("form-label").text("타겟 테이블 ID컬럼명");
    form_group_5.append(label_5);

    var input_tag_5 = $("<input/>").addClass("form-control").attr({
        "type":"text",
        "id":"targetIdColumn",
        "name":"example-text-input",
        "placeholder":"타겟 테이블 ID컬럼.."
    });
    form_group_5.append(input_tag_5);

    var form_group_5_2 = $("<div/>").addClass("form-group");
    div_card.append(form_group_5_2);

    var label_5_2 = $("<label/>").addClass("form-label").text("타겟 테이블 제목컬럼명");
    form_group_5_2.append(label_5_2);

    var input_tag_5_2 = $("<input/>").addClass("form-control").attr({
        "type":"text",
        "id":"targetTitleColumn",
        "name":"example-text-input",
        "placeholder":"타겟 테이블 제목컬럼.."
    });
    form_group_5_2.append(input_tag_5_2);

    var form_group_6 = $("<div/>").addClass("form-group");
    div_card.append(form_group_6);

    var label_6 = $("<label/>").addClass("form-label").text("타겟 학습컬럼명");
    form_group_6.append(label_6);

    var input_tag_6 = $("<input/>").addClass("form-control").attr({
        "type":"text",
        "id":"targetTrainingColumn",
        "name":"example-text-input",
        "placeholder":"타겟 학습컬럼.."
    });
    form_group_6.append(input_tag_6);

    var form_group_7 = $("<div/>").addClass("form-group");
    div_card.append(form_group_7);

    var label_7 = $("<label/>").addClass("form-label").text("저장 테이블명");
    form_group_7.append(label_7);

    var input_tag_7 = $("<input/>").addClass("form-control").attr({
        "type":"text",
        "id":"saveTable",
        "name":"example-text-input",
        "placeholder":"저장 테이블.."
    });
    form_group_7.append(input_tag_7);

    var w2v_table_btn = $("<input/>").addClass("btn btn-primary col-2").attr({
        "id":"w2vStartBtn"
    }).val("w2vec시작");
    div_card.append(w2v_table_btn);

    var table_btn = $("<input/>").addClass("btn btn-primary col-2").attr({
        "id":"d2vStartBtn"
    }).css("margin-left", "5px").val("doc2vec시작");
    div_card.append(table_btn);

    $("#d2vStartBtn").off('click').on('click', function(e) {
        d2vStart();
    });

    $("#w2vStartBtn").off('click').on('click', function(e) {
        w2vStart();
    });
}

function d2vStart() {

    var SelectTableVO = {};
    var DocSettingVO = {};
    var settingVO = {};

    SelectTableVO['sourceTableName'] = $("#domainTableName").val();
    SelectTableVO['sourceTableColumn'] = $('#domainTrainingColumn').val();
    SelectTableVO['sourceTableKeywordColumn'] = "1";
    SelectTableVO['targetTableName'] = $('#targetTableName').val();
    SelectTableVO['targetTableColumn'] = $('#targetTrainingColumn').val();
    SelectTableVO['targetTableKeywordColumn'] = "1";

    DocSettingVO['sourceDocIdColumn'] = $("#domainIdColumn").val();
    DocSettingVO['sourceDocNameColumn'] = $("#domainTitleColumn").val();
    DocSettingVO['targetDocIdColumn'] = $("#targetIdColumn").val();
    DocSettingVO['targetDocNameColumn'] = $("#targetTitleColumn").val();

    settingVO['selectTableVO'] = SelectTableVO;
    settingVO['docSettingVO'] = DocSettingVO;
    settingVO['jUrl'] = $("#jUrl").val();
    settingVO['saveTableNm'] = $("#saveTable").val();

    var url = contextPath + '/api/d2v/d2vStart';
    $.ajax({
        url: url,
        type: "POST",
        data:JSON.stringify(settingVO),
        contentType : 'application/json',
        async: true,
        dataType: 'json',
        success: function (result) {
        }
    });
    d2vProgress();
}

function w2vStart() {
    var url = contextPath + '/api/d2v/w2vStart';
    $.ajax({
        url: url,
        type: "POST",
        data:{
            "jUrl":$("#jUrl").val()
        },
        async: true,
        success: function (result) {
        }
    });
}

function d2vProgress() {
    var url = contextPath + '/api/d2v/d2vProgress';
    $.ajax({
        url: url,
        type: "POST",
        contentType : 'application/json',
        async: true,
        dataType: 'json',
        success: function (result) {
            var progressInt = parseInt((result[0] * 1 / result[1] * 1) * 100);
            if(!isNaN(progressInt) && result[0] != 0) {
                var progressPer = progressInt + "%";
                var width = progressInt + "%";
            }
            else if((isNaN(progressInt) || result[0] == 0) && result[2] == "") {
                var progressPer = "Loading...";
                var width = "0%";
            }
            if(result[2] != "") {
                var progressPer = result[2];
                var width = "0%";
            }

            $("#progressBar").attr('aria-valuenow', progressInt);
            $("#progressBar").css('width', width);
            $("#progressText").text(progressPer);
            $("#progressCount").text(result[0] + " / " + result[1]);
            if(progressInt != 100) d2vProgress();
        }
    });
}