let options = {
    diapason_akk: [],
    time_start: [2,5],
    phrases_list: [],
    num_post_reactions: '{👍|😍|👏|🔥|🎉|❤️|🥰|😁}',
    num_post_depth: [5,20],
    timer_mode: 0,
    timer_mode_num: "",
};

$('#activity_start').on('click', async function () {
    //{"chats":["@dfgggh",http....","t.me..."],"online":[0,48],"type":["admin","bots","alluser"]}
    //Получение данных из textarea
    options.phrases_list = [];
    let lines = $('#username_textarea_phrases').val().split(/\n/);
    for (var i = 0; i < lines.length; i++) {
      if (/\S/.test(lines[i])) {
        options.phrases_list.push($.trim(lines[i]));
      }
    }

    if ($('#timer_mode').is(':checked')){
      options.timer_mode = 1;
      options.timer_mode_num = $('#num_timer').val();
    } else {
        options.timer_mode = 0;
    }

    let time_start_from = $('#time_start_from').val();
    let time_start_before = $('#time_start_before').val();
    if(parseInt(time_start_from) >= parseInt(time_start_before)){
        error('Значение времени "до" не может быть меньше!')
        return
    } else {
        options.time_start[0] = time_start_from;
        options.time_start[1] = time_start_before;
    }
  
    let num_post_reactions = $('#num_post_reactions').val();
    if (num_post_reactions !== "") {
      options.num_post_reactions = $('#num_post_reactions').val();
    } else {
      options.num_post_reactions = "{👍|😍|👏|🔥|🎉|❤️|🥰|😁}";
    }

    // let num_post_depth = $('#num_post_depth').val();
    // if (num_post_depth !== "") {
    //   options.num_post_depth = $('#num_post_depth').val();
    // } else {
    //   options.num_post_depth = "20";
    // }

    let num_post_depth_from = $('#num_post_depth_from').val();
    let num_post_depth_before = $('#num_post_depth_before').val();
    if(parseInt(num_post_depth_from) >= parseInt(num_post_depth_before)){
        error('Значение времени "до" не может быть меньше!')
        return
    } else {
        options.num_post_depth[0] = num_post_depth_from;
        options.num_post_depth[1] = num_post_depth_before;
    }

    clear_log = 0
    let json = JSON.stringify(options);
    //Получили json обьект
    let this_class = $(this).hasClass("btn-success");
    if (this_class == true) {
        $(this).removeClass('btn-success');
        $(this).addClass('btn-danger');
        $(this).text("Остановить");
        $('#log li').remove();
        await eel.reaction_cheat(json)();
    } else {
        $(this).removeClass('btn-danger');
        $(this).addClass('btn-success');
        $(this).text("Запустить");
        eel.stopped('reaction_cheat') //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ТУТ КНОПКА ОТМЕНИТЬ
    }

});

window.addEventListener("beforeunload", function (e) {        
    eel.stopped('reaction_cheat')
});

function get_num_user_list(){
    let lines = $('#username_textarea_phrases').val().split(/\n/).length;
    $("#num_textarea_phrases").text(lines);
}

$('#save_phrases').on('click', async function () {
    save_login_history();
    get_num_user_list();
    success('Каналы сохранены!');
})

$('#username_textarea_phrases').bind('input', function(){
    get_num_user_list();
});

function save_login_history(){
    let lines = $('#username_textarea_phrases').val()
    localStorage.setItem('textarea_phrases_cheat', lines);
}

$('#get_phrases_history').on('click', function(){
    let username_list = localStorage.getItem('textarea_phrases_cheat');
    $('#username_textarea_phrases').val(username_list)
    get_num_user_list();
  });

eel.expose(set_finished_btn);
function set_finished_btn(){
    $('#activity_start').removeClass('btn-danger');
    $('#activity_start').addClass('btn-success');
    $('#activity_start').text("Запустить");
}

let clear_log = 0
eel.expose(log);
function log(log_line) {
  if (clear_log == 250){
    $('#log li:first').remove();
  } else {clear_log = clear_log + 1}
  $('#log').append('<li>' + log_line + '</li>');
  $('#log_div').scrollTop($('#log_div')[0].scrollHeight);
}




$('#select_accounts').on('click', async function () {
    options.diapason_akk = [];
    load_accounts();
});

$('#select_accounts_apply').on('click', async function () {
    $('#select_accounts_modal').modal('hide');
    let from = 1;
    let before = $('#table_users >tbody >tr').length;
    //Запускаем цикл по всей таблице
    for (let i = from; i <= before; i++) {
        let type_bg = $('#table_users tr').eq(i).attr("class");
        if (type_bg == "bg_selected") {
        //Получаем номер телефона
        phone_user = $('#table_users tr').eq(i).children('#phone').html();
        options.diapason_akk.push(phone_user);
        }
    }
    $("#account_num").removeClass('d-none');
    $("#account_num").text(options.diapason_akk.length);
});

$('#timer_mode').on('click', function () {
  if ($('#timer_mode').is(':checked')){
      $('.timer_mode_input').removeClass('d-none');
  } else {
      $('.timer_mode_input').addClass('d-none');
  }
})

// eel.expose(error);
function error(text) {
    Swal.fire({
        position: 'center',
        icon: 'error',
        title: 'Ошибка',
        text: text,
        showConfirmButton: false,
        timer: 2100
    })
}