jQuery(document).ready(function($) {

    var ahachatShowHideMessageTagText = function() {
        var val = $('#abd_send_message_by_tag').val();

        $('.ahachat_message_tag_tut').hide();
        $('#' + val + '_TUT').show();
    };

    ahachatShowHideMessageTagText();
    $(document).on('change', '#abd_send_message_by_tag', function(event) {
        ahachatShowHideMessageTagText();
    });

    $(document).on('click', '.abd_sen_message', function(event) {
        event.preventDefault();
        var check_checkbok = $('input[name="listuser[]"]').is(':checked');
        if (check_checkbok) {
            var html = '<option value="' + getUserCheck() + '">Selected Users (' + getUserCheck().length + ')</option>';
            $("#ahachat_send_message_to_person").append(html);
        } else {
            var html = '<option value="0">Selected Users (' + getUserCheck().length + ')</option>';
            $("#ahachat_send_message_to_person").append(html);
        }
        $('.ahachat_spider_create_group_popup').addClass('spider-open');
    });

    $(document).on('click', 'span.tb-close-icon', function(e) {
        e.preventDefault();
        $('.ahachat_spider_create_group_popup').removeClass('spider-open');
        $('#ahachat_send_message_to_person').find('option:last-child').remove();
        $("#ahachat_first_send").show();
        $("#ahachat_second_send").hide();
        $(".ahachat_first_send_complete").hide();
    });

    function getUserCheck() {
        var selected_u = [];
        $('.listuser .check-column > input[name^="listuser"]').each(function(i, el) {
            if ($(el).is(':checked')) {
                selected_u.push($(el).val());
            }
        });
        return selected_u;
    }

    $('.ahachat-fb-mess-view-fail-detail').on('click', function() {
        $('.ahachat-mess-fail-details').slideToggle();
    });

    var array_list_user = [];
    var success = 0;
    var fail = 0;
    var percent = 0;
    var percent_check = 0;
    var mesage_messenger = "";
    var message_tag = "";
    $(document).on('click', '.abd_send_message_again', function(e) {
        e.preventDefault();

        array_list_user = []; // Avoid clicking multiple times

        success = 0; // Avoid clicking multiple times

        fail = 0; // Avoid clicking multiple times

        percent = 0; // Avoid clicking multiple times

        percent_check = 0; // Avoid clicking multiple times

        mesage_messenger = "";

        $(".ahachat_send_success_first").text(success);

        $(".ahachat_send_fail_first").text(fail);

        $("ul.ahachat-mess-fail-details li").remove(); // Avoid clicking multiple times

        $("#ahachat_progress_send_mess").val(percent_check); // Avoid clicking multiple times

        var value = $('#ahachat_send_message_to_person').val();

        var content_mess = $('#ahachat_content_mess').val();

        message_tag = $('#abd_send_message_by_tag').val();

        mesage_messenger = content_mess;

        if (mesage_messenger == "") {
            alert("please input content message!");
            return false;
        }

        if (value == 0) {
            alert("Please checked user send messager!");

            return false;
        } else if (value == "all") {
            var action_data = {
                action: 'ahachat_get_all_user_add',
                id_user_send: value,
            };

            $.post(ajaxurl, action_data, function(result) {
                if (result) {
                    if (result == "error") {
                        alert("Not User Found");

                        return false;
                    } else {
                        var list_user = result.split(",");
                        percent = 100 / (list_user.length);
                        $.each(list_user, function(i1, v1) {
                            array_list_user.push(v1);
                        });
                        call_ajax_first_end();
                    }
                }

            });
        } else {
            var list_user = value.split(",");
            percent = 100 / (list_user.length);
            $.each(list_user, function(i1, v1) {
                array_list_user.push(v1);
            });
            call_ajax_first_end();
        }

        return false;
    });


    function call_ajax_first_end() {
        $("#ahachat_first_send").hide();
        $("#ahachat_second_send").show();
        $(".ahachat_first_send_complete").show();

        ahachat_send_message_user(0, function() {
            $("#ahachat_first_send").show();
            $("#ahachat_second_send").hide();
            $("h3#abd_send_complete").text("Complete");
        });

    }

    function ahachat_send_message_user(index, callback) {
        if (typeof array_list_user[index] != 'undefined')
        {

            $.ajax({
                    url: ajaxurl + '?action=ahachat_send_one_end',
                    type: 'POST',

                    data: {
                        'id_user_send': array_list_user[index],
                        'mesage_messenger': mesage_messenger,
                        message_tag: message_tag
                    },

                })
                .done(function(status) {
                    var temp = percent;
                    percent_check += temp;

                    $("#ahachat_progress_send_mess").val(percent_check);
                    if (status == "error") {
                        console.log("error");
                    } else if (status == "done") {
                        success++;
                        $(".ahachat_send_success_first").text(success);
                    } else {
                        fail++;
                        $(".ahachat_send_fail_first").text(fail);
                        $("ul.ahachat-mess-fail-details").append('<li>' + status + '</li>');
                    }
                    ahachat_send_message_user(index + 1, callback);

                })

                .fail(function() {
                    console.log("error");
                });
        } else {
            callback();
        }
    }
});