jQuery(document).ready(function ($) {
    var widgetType = '';
    var widgetType = $(".cmb2-id-type select#type").val();
    var sField = $(".cmb2-id-display-currencies");
    var showCoinsSettings = $("#show-coins");

    //replace wallet titles with selected coin
    var replaceTitles = function () {
        var $box = $(document.getElementById('donation_group_data_repeat'));
        $box.find('.cmb-group-title').each(function () {
            var $this = $(this);
            var radio_val = $this.next().find('[type="radio"]')[1];
            if (radio_val.getAttribute('checked') == "checked") {
                var txt = $this.next().find("select[data-conditional-value='coin_type_metamask'] option:selected").text();
                var rowindex;
                if (!txt) {
                    txt = $box.find('[data-grouptitle]').data('grouptitle');
                    if (txt) {
                        rowindex = $this.parents('[data-iterator]').data('iterator');
                        txt = txt.replace('{#}', (rowindex + 1));
                    }
                }

            }
            else{
                var txt = $this.next().find("select[data-conditional-value='coin_type_popular'] option:selected").text();

                var rowindex;
                if (!txt) {
                    txt = $box.find('[data-grouptitle]').data('grouptitle');
                    if (txt) {
                        rowindex = $this.parents('[data-iterator]').data('iterator');
                        txt = txt.replace('{#}', (rowindex + 1));
                    }
                }

            }

            if (txt) {
                $this.text(txt);
            }

            
        });
    };
    replaceTitles();
    
    // Replace the names after click on Remove button
    $(".dashicons-before.dashicons-no-alt.cmb-remove-group-row, .cmb-remove-group-row.cmb-remove-group-row-button.alignright.button-secondary").on("click", function() {
        setTimeout(() => {
            replaceTitles();
        }, 1);
    });



    switch (widgetType) {
        case "table-widget":
        case "calculator":
        case "changelly-widget":
        case "rss-feed":
        case "technical-analysis":
            sField.hide();
            break;
        case "chart":
            $("#show-coins option").not(":first").each(function () {
                $(this).attr("disabled", true);
            });
            break;
        default:
            // sField.show();
            $("#show-coins option").not(":first").each(function () {
                $(this).attr("disabled", false);
            });
            //   $("#display_currencies").attr("required",false);
            break;
    
    }

    $(".cmb2-id-type select#type").change(function () {
        var widgetType = $(this).children("option:selected").val();
       
        if (widgetType == "chart") {
            $("#show-coins option").not(":first").each(function () {
                $(this).attr("disabled", true);
            });

        } else {
            $("#show-coins option").not(":first").each(function () {
                $(this).attr("disabled", false);
            });
        }
    });

    var showCoinsVal = $("#show-coins").val();
    if (showCoinsVal == "custom") {
        // $("#display_currencies").attr("required",true);
    }

    $("#show-coins").change(function () {
        var selVal = $(this).children("option:selected").val();
        if (selVal == "custom") {
            //    $("#display_currencies").attr("required",true)
        } else {
            //  $("#display_currencies").attr("required",false)
        }
    });

     
    //calling hide show fields function
    setTimeout(() => {
        let widgetTypes = $(".cmb2-id-type select#type").children("option:selected").val();
        hideShowFields(widgetTypes)
    }, 100);

    

    $(".cmb-type-select select.cmb2_select").change(function () {
        
        setTimeout(() => {
            let widgetTypes = $(".cmb2-id-type select#type").children("option:selected").val();
          
            hideShowFields(widgetTypes)
        }, 100);
        
    })


    function hideShowFields(type) {        
        $(".cmb2-id-donation-group-data").hide();
        $(".cmb2-id-metamask-transactions").hide();        
        $('.cmb2-id-card-display-changes').hide();
        $('.cmb2-id-ccpw-popular-title').hide();
        $('.cmb2-id-ccpw-metamask-title').hide();
        $('.cmb2-id-ccpw-donation-metamask-title').hide();
        $('.cmb2-id-ccpw-donation-style-title').hide();
        $('.cmb2-id-custom-css').show()
        let LiveChanges = $('.cmb2-id-live-changes').is(':visible')   
        if (LiveChanges){
            if ($('.cmb2-id-live-changes li input:checked').val()=="on")$('.cmb2-id-binance-url').show()
            else $('.cmb2-id-binance-url').hide()

            $('.cmb2-id-live-changes li input').click(function (obj) {
                if ($(this).val()=="on") $('.cmb2-id-binance-url').show()
                else $('.cmb2-id-binance-url').hide()                
            })
        }
        
        switch (type) {
            case "ticker":
                
                let Tickefield = $(".cmb2-id-design-ticker").is(':visible')
                let Tickerdesign = $(".cmb2-id-design-ticker select#design_ticker").children("option:selected").val();
                $('.cmb2-id-display-format').hide()
                if (Tickerdesign == 'style-1' && Tickefield == true)
                    $('.cmb2-id-display-format').show()
                break;
            case "accordion-block":
                let Accordianfield = $(".cmb2-id-accordion-block-design").is(':visible')
                let Accordiandesign = $(".cmb2-id-accordion-block-design select#accordion-block-design").children("option:selected").val();
                $('.cmb2-id-display-format').hide()
                if (Accordiandesign == 'style-3' && Accordianfield == true)
                    $('.cmb2-id-display-format').show()
                break;
            case "price-block":
                let Blockfield = $(".cmb2-id-design-block").is(':visible')
                let Blockdesign = $(".cmb2-id-design-block select#design_block").children("option:selected").val();
                $('.cmb2-id-display-format').hide()
                $('.cmb2-id-block-chart-color').show()
                $('.cmb2-id-block-chart-fill').hide()
                $('.cmb2-id-price-block-currency').show()
                $('.cmb2-id-disable-bootstrap').show()
                if (Blockdesign == 'style-1'){
                    $('.cmb2-id-disable-bootstrap').hide()
                    $('.cmb2-id-block-chart-fill').show()
                }
                else if ((Blockdesign == 'style-2'||Blockdesign == 'style-3' || Blockdesign == 'style-4' ) && Blockfield == true)
                    $('.cmb2-id-display-format').show()
                else if ((Blockdesign == 'style-5') && Blockfield == true) {
                    $('.cmb2-id-disable-bootstrap').hide()
                    $('.cmb2-id-block-chart-color').hide()                   
                    $('.cmb2-id-price-block-currency').hide()
                }
                break;
            case "price-card":
                let Cardfield = $(".cmb2-id-design-card ").is(':visible')
                let Designcard = $(".cmb2-id-design-card select#design_card").children("option:selected").val();
                $('.cmb2-id-display-format').hide()
                $('.cmb2-id-card-display-changes').hide()
                if (Designcard == 'style-2' && Cardfield == true) {
                    $('.cmb2-id-card-display-changes').show()
                    $('.cmb2-id-display-format').show()
                }
                else if ((Designcard != 'style-3' && Designcard != 'style-7') && Cardfield == true)
                    $('.cmb2-id-card-display-changes').show()
                break;
            case "price-label":
                let LabelField = $(".cmb2-id-design").is(':visible')
                let Labeldesign = $(".cmb2-id-design select#design").children("option:selected").val();
                $('.cmb2-id-label-list-display-changes').show()
                if (Labeldesign == "style-5" && LabelField == true) {
                    $('.cmb2-id-label-list-display-changes').hide()
                }
                break;

            case "list-widget":
                let ListField = $(".cmb2-id-design").is(':visible')
                let Listdesign = $(".cmb2-id-design select#design").children("option:selected").val();
                $('.cmb2-id-display-format').hide()
                $('.cmb2-id-label-list-display-changes').show()
                if (Listdesign == 'style-2' && ListField == true)
                    $('.cmb2-id-display-format').show()
                else if ((Listdesign == 'style-5') && ListField == true)
                    $('.cmb2-id-label-list-display-changes').hide()
                break;
            case "slider-widget":
                let sliderField = $(".cmb2-id-design-slider").is(':visible')
                let Sliderdesign = $(".cmb2-id-design-slider select#design_slider").children("option:selected").val();
                $('.cmb2-id-slider-chart-color').hide()
                if (Sliderdesign == "style-3" && sliderField == true) {
                    $('.cmb2-id-slider-chart-color').show()
                }
                break;
            case "table-widget":
                break;
            case "multi-currency-tab":
                break;
            case "calculator":
                break;
            case "rss-feed":
                break;
            case "binance-live-widget":
                break;
            case "coingecko-widget":
                break;
            case "changelly-widget":
                break;
            case "donation-widget":
                $(".cmb2-id-donation-group-data").show();   //hiding wallet
                $(".cmb2-id-metamask-transactions").show(); 
                 $('.cmb2-id-ccpw-popular-title').show();
                $('.cmb2-id-ccpw-metamask-title').show();
                 $('.cmb2-id-ccpw-donation-metamask-title').show();
                $('.cmb2-id-ccpw-donation-style-title').show();
                break;   
            case "technical-analysis":
                $('.cmb2-id-custom-css').hide()
                break;
            case "chart":
                $(".cmb2-id-show-coins select#show-coins").children("option:first").attr("selected", "selected");
                $('.cmb2-id-display-currencies').show();
                $('.cmb2-id-custom-coins-id').show();
                break;
            default:
                break;

        }//end of switch

    }// end of hideshowfields function
});
