$(function () {

    if (!$('.calculator').length) {
        return;
    }

    function set_plan(planId) {

        var parsedId = parseInt(planId);
        if (isNaN(parsedId)) {
            console.error('Wrond planId. (' + planId + ')');
            return;
        }

        $('form input[name="enter[plan]"]').val(parsedId);
    }

    function get_selected_plan() {

        var planId = $('.calculator select[name="enter[plan]"]').val();
        var planInt = parseInt(planId);

        return planInt;
    }

    function get_selected_ps() {

        var psId = $('.calculator input[name="enter[ps]"]:checked').val();
        var psInt = parseInt(psId);

        return psInt;
    }

    function get_ps_currency() {
        var input = $('.calculator input[name="enter[ps]"]:checked');
        var currecy = input.data('currency');

        return currecy;
    }

    function get_full_profit_percent() {
        var input = $('.calculator select[name="enter[plan]"] option:selected');
        var profitPercent = parseFloat(input.data('total-percent').toFixed(8));

        return profitPercent;
    }

    function get_calc_profit_text() {
        var input = $('.calculator select[name="enter[plan]"] option:selected');
        var text = input.data('profit-text');

        return text;
    }

    function get_ps_icon_class() {
        var input = $('input[name="enter[ps]"]:checked');
        var icon = input.data('input-icon-class');

        return icon;
    }

    function get_ps_icon() {
        var input = $('input[name="enter[ps]"]:checked');
        var icon = input.data('icon');

        return icon;
    }

    function get_limit_min_by_id(plan_id, ps_id) {

        var limits = window.plan_limits_json_parsed;
        var min_amount = 0;

        try {
            if (limits[plan_id][ps_id]) {
                min_amount = parseFloat(limits[plan_id][ps_id]['min']).toFixed(8);
            }
        } catch (e) {
            console.error(e);
        }


        min_amount = parseFloat(min_amount);
        return min_amount;
    }

    function get_limit_max_by_id(plan_id, ps_id) {

        var limits = window.plan_limits_json_parsed;
        var max_amount = 0;

        try {
            if (limits[plan_id][ps_id]) {
                max_amount = parseFloat(limits[plan_id][ps_id]['max']).toFixed(8);
            }
        } catch (e) {
            console.error(e);
        }

        max_amount = parseFloat(max_amount);
        return max_amount;
    }

    function get_amount() {
        var input = $('form input[name="enter[amount]"]');
        var amount = parseFloat(input.val());
        if (isNaN(amount)) {
            amount = 0;
        }

        return amount;
    }

    function set_amount(amount) {

        if (amount.toString().indexOf(',') !== -1) {
            amount = amount.toString().replace(',', '.');
        }

        var input = $('.calculator input[name="enter[amount]"]');
        input.val(amount);
    }

    function set_profit(amount, currency) {

        amount = parseFloat(amount.toFixed(8));

        var text = amount + ' ' + currency;
        $('.calculator .set_profit').html(text);
    }


    function initForm() {

        var form = $('.calculator');

        var planId = get_selected_plan();
        var psId = get_selected_ps();
        var currency = get_ps_currency();
        var iconClass = get_ps_icon_class();

        var iconWrapper = $('.calculator .input-wrapper');
        iconWrapper.removeClass();
        iconWrapper.addClass('input-wrapper ' + iconClass);

        var minAmount = get_limit_min_by_id(planId, psId);
        set_amount(minAmount);

        var fullProfitPercent = get_full_profit_percent();
        var fullProfitAmount = (minAmount / 100) * fullProfitPercent;
        set_profit(fullProfitAmount, currency);

        var profitText = get_calc_profit_text();
        form.find('.set_profit_text').html(profitText);

        console.log('initForm executed.');
    }

    $('.calculator input[name="enter[amount]"]').keyup(function () {
        set_amount($(this).val());

        var amount = get_amount();
        var currency = get_ps_currency();

        var fullProfitPercent = get_full_profit_percent();
        var fullProfitAmount = (amount / 100) * fullProfitPercent;

        set_profit(fullProfitAmount, currency);
    });

    $('.calculator input[name="enter[ps]"]').change(function () {
        initForm();
    });

    $('.calculator select[name="enter[plan]"]').change(function () {
        initForm();
    });

    initForm();





});