import _ from 'lodash';

import {Alert} from './popups/Alert';
import {AutosuggestControl} from './form/AutosuggestControl';
import {Confirm} from './popups/Confirm';
import {PurePagination} from './list/PurePagination';
import {HeaderToolbar} from './HeaderToolbar';
import {Note} from './Note';
import {CounterText} from './list/CounterText';
import {PureListLayout} from './list/PureListLayout';
import {ListLayout} from './list/withStore/ListLayout';
import {SelectedRecordsSwitcher} from './SelectedRecordsSwitcher';
import {PureList} from './list/PureList';
import {List} from './list/withStore/List';
import {CommonListSorter} from './list/common/CommonListSorter';
import {GridHeaderCell} from './list/withStore/GridHeaderCell';
import {Wizard} from './Wizard';
import {WizardStep} from './WizardStep';
import {MultiSelect} from './form/MultiSelect';
import {Menu} from './Menu';
import {FileUploadControl} from './form/FileUploadControl';
import {DatePickerControl} from './form/DatePickerControl';
import {DateRangePickerCtrl} from './form/DateRangePickerControl';
import {AudioPlayer} from './media/AudioPlayer';
import {AudioRecorder, RecorderVolumeMeter, RecorderProcessor} from './media/AudioRecorder';
import {CollapsibleToolbar} from './CollapsibleToolbar';
import {Dropzone} from './Dropzone';
import {VolumeControl} from './media/VolumeControl';
import {Panel} from './Panel';
import {Filter} from './Filter';
import {SplitButton} from './SplitButton';
import {Banner} from './Banner';
import {Popup} from './popups/Popup';
import {TiledGrid} from './TiledGrid';
import {ListControlCheckbox} from './list/withStore/ListControlCheckbox';
import {InputCounterControl} from './form/InputCounterControl';
import {CTADropdown} from './CTADropdown';
import {SingleDatePickerInputControl} from './form/DateTimePicker/SingleDatePickerInputControl';
import {DayRangePickerControl} from './form/DateTimePicker/DayRangePickerControl';
import {TreeFilterControl} from './form/TreeFilterControl';
import {DateRangePickerWithPresets} from './form/DateRangePickerWithPresets';
import {CatalogListDetails, CatalogSimplePrice} from './catalog';
import {Search} from './Search';
import {ToolsMenu} from './ToolsMenu';

import {
    keyboardSortableDesc,
    keyboardSortableListItemDefaultProps
} from '../../dnd/keyboardSortableListItem';

export {
    showPopup,
    closePopup,
    showAlert,
    showConfirmation,
    showNotification,
    scrollToTop,
    getScrollbarWidth
} from './utils';

export {playMessage} from '../utils/playMessage';

export function localize(strings) {
    replaceUnTranslatedDefaultProps(SingleDatePickerInputControl, strings);
    replaceUnTranslatedString(SingleDatePickerInputControl.defaultProps.dayPickerPhrase, strings);

    replaceUnTranslatedDefaultProps(DayRangePickerControl, strings);
    replaceUnTranslatedString(DayRangePickerControl.defaultProps.dayPickerPhrase, strings);

    replaceUnTranslatedDefaultProps(DateRangePickerWithPresets, strings);

    PurePagination.defaultProps.allText = strings.ALL;
    PurePagination.defaultProps.pageSizeSelectorText = strings.SHOW;
    PurePagination.defaultProps.goToPageLabel = strings.GO_TO_PAGE;
    PurePagination.defaultProps.currentPageLabel = strings.CURRENT_PAGE;
    PurePagination.defaultProps.pageNextLabel = strings.GO_TO_NEXT_PAGE;
    PurePagination.defaultProps.pagePrevLabel = strings.GO_TO_PREV_PAGE;

    replaceUnTranslatedDefaultProps(MultiSelect, strings);
    replaceUnTranslatedDefaultProps(TreeFilterControl, strings);
    replaceUnTranslatedDefaultProps(Menu, strings);

    HeaderToolbar.defaultProps.backText = strings.BACK;
    HeaderToolbar.defaultProps.detailSettingForArialLabel = strings.DETAIL_SETTING_FOR_ARIA_LABEL;
    Note.defaultProps.noteText = strings.NOTE;
    SelectedRecordsSwitcher.defaultProps.showAllText = strings.SHOW_ALL;
    SelectedRecordsSwitcher.defaultProps.showSelectedText = strings.SHOW_SELECTED;
    CounterText.defaultProps.template = strings.TOTAL_AMOUNT;
    PureListLayout.defaultProps.storeCounterText = strings.TOTAL_AMOUNT;
    ListLayout.defaultProps.storeCounterText = strings.TOTAL_AMOUNT;
    Wizard.confirmWizardRestore = strings.CONFIRM_WIZARD_RESTORE;
    WizardStep.defaultProps.backText = strings.BACK;
    WizardStep.defaultProps.nextText = strings.NEXT;
    WizardStep.defaultProps.cancelText = strings.CANCEL;
    PureList.defaultProps.noResultsText = strings.NO_RESULTS;
    PureList.defaultProps.blockedText = strings.PLEASE_WAIT_WHILE_LOADING;
    List.defaultProps.noResultsText = strings.NO_RESULTS;
    List.defaultProps.blockedText = strings.PLEASE_WAIT_WHILE_LOADING;
    Alert.defaultProps.title = strings.ALERT;
    Alert.defaultProps.submitText = strings.OK;
    CollapsibleToolbar.defaultProps.moreButtonText = strings.MORE;
    CollapsibleToolbar.defaultProps.moreButtonLabel = strings.MORE_ADVANCED_FILTERS;
    Confirm.defaultProps.title = strings.CONFIRMATION;
    Confirm.defaultProps.submitText = strings.YES;
    Confirm.defaultProps.cancelText = strings.NO;
    AudioPlayer.defaultProps.loadingError = strings.CANNOT_OPEN_AUDIO;
    AudioPlayer.defaultProps.playLabel = strings.PLAY;
    AudioPlayer.defaultProps.pauseLabel = strings.PAUSE;
    AudioPlayer.defaultProps.timeSlider = strings.TIMELINE_SLIDER;
    AudioPlayer.defaultProps.recordLabel = strings.RECORD;
    AudioPlayer.defaultProps.downloadLabel = strings.DOWNLOAD;
    FileUploadControl.defaultProps.uploadText = strings.UPLOAD;
    AudioRecorder.defaultProps.unsupportText = strings.MICROPHONE_NOT_SUPPORTED;
    AudioRecorder.defaultProps.volumeMeterTitle = strings.MICROPHONE_TEST;
    AudioRecorder.defaultProps.volumeMeterText = strings.MICROPHONE_TEST_TEXT;
    AudioRecorder.defaultProps.processorTitle = strings.RECORDING;
    AudioRecorder.defaultProps.processorText = strings.RECORDING_IN_PROGRESS;
    AudioRecorder.defaultProps.completedTitle = strings.RECORDING;
    AudioRecorder.defaultProps.completedText = strings.MICROPHONE_RECORD_COMPLETE;
    AudioRecorder.defaultProps.audioRecordError = strings.MICROPHONE_RECORD_ERROR;
    AudioRecorder.defaultProps.deviceNotFoundError = strings.MICROPHONE_NOT_FOUND_ERROR;
    AudioRecorder.defaultProps.uploadLabel = strings.UPLOAD_RECORD;
    AutosuggestControl.defaultProps.suggestionPromptLabel =
        strings.AUTO_SUGGEST_SUGGESTION_PROMPT_LABEL;
    AutosuggestControl.defaultProps.noSuggestionPromptLabel = strings.NO_SUGGESTION_PROMPT_LABEL;
    CommonListSorter.defaultProps.ascendingText = strings.SORT_BY_ASCENDING;
    CommonListSorter.defaultProps.descendingText = strings.SORT_BY_DESCENDING;
    CommonListSorter.defaultProps.sorterTitleText = strings.CLICK_TO_SORT;
    GridHeaderCell.defaultProps.ascendingText = strings.SORT_BY_ASCENDING;
    GridHeaderCell.defaultProps.descendingText = strings.SORT_BY_DESCENDING;
    GridHeaderCell.defaultProps.sorterTitleText = strings.CLICK_TO_SORT;
    VolumeControl.defaultProps.muteLabel = strings.MUTE;
    VolumeControl.defaultProps.unmuteLabel = strings.UNMUTE;
    VolumeControl.defaultProps.volumeSlider = strings.VOLUME_SLIDER;
    RecorderVolumeMeter.defaultProps.startRecordingLabel = strings.START_RECORDING;
    RecorderProcessor.defaultProps.stopRecordingLabel = strings.STOP_RECORDING;
    Panel.defaultProps.closeLabel = strings.CLOSE;
    DateRangePickerCtrl.defaultProps.ariaLabel = strings.CHOOSE_DATE_RANGE;
    DateRangePickerCtrl.defaultProps.setPrevMonthText = strings.SET_PREV_MONTH;
    DateRangePickerCtrl.defaultProps.setNextMonthText = strings.SET_NEXT_MONTH;
    DatePickerControl.defaultProps.setPrevMonthText = strings.SET_PREV_MONTH;
    DatePickerControl.defaultProps.setNextMonthText = strings.SET_NEXT_MONTH;
    DatePickerControl.defaultProps.setPrevYearText = strings.SET_PREV_YEAR;
    DatePickerControl.defaultProps.setNextYearText = strings.SET_NEXT_YEAR;
    DatePickerControl.defaultProps.setPrevDecadeText = strings.SET_PREV_DECADE;
    DatePickerControl.defaultProps.setNextDecadeText = strings.SET_NEXT_DECADE;
    DatePickerControl.defaultProps.changeMonthText = strings.CHANGE_MONTH;
    DatePickerControl.defaultProps.changeYearText = strings.CHANGE_YEAR;
    DatePickerControl.defaultProps.setPrevHourText = strings.SET_PREV_HOUR;
    DatePickerControl.defaultProps.setNextHourText = strings.SET_NEXT_HOUR;
    DatePickerControl.defaultProps.setPrevMinuteText = strings.SET_PREV_MINUTE;
    DatePickerControl.defaultProps.setNextMinuteText = strings.SET_NEXT_MINUTE;
    DatePickerControl.defaultProps.toggleAMPMText = strings.TOGGLE_AM_PM;
    Filter.defaultProps.ariaLabel = strings.SEARCH;
    SplitButton.defaultProps.splitToggleLabel = strings.MORE_OPTIONS;
    Banner.defaultProps.closeLabel = strings.CLOSE;
    Popup.defaultProps.closeLabel = strings.CLOSE;
    Dropzone.defaultProps.orText = strings.OR;
    Dropzone.defaultProps.dragText = strings.DRAG_FILES_HERE;
    TiledGrid.defaultProps.closeLabel = strings.CLOSE;
    InputCounterControl.defaultProps.decreaseLabel = strings.DECREASE;
    InputCounterControl.defaultProps.increaseLabel = strings.INCREASE;
    CTADropdown.defaultProps.toggleMoreLabel = strings.MORE_SETTINGS;
    ListControlCheckbox.defaultProps.label = strings.SELECT_ALL;
    CatalogListDetails.defaultProps.textDetails = strings.DETAILS;
    CatalogListDetails.defaultProps.textPurchase = strings.PURCHASE;
    CatalogSimplePrice.defaultProps.textFrom = strings.FROM;
    CatalogSimplePrice.defaultProps.textOr = strings.OR;
    CatalogSimplePrice.defaultProps.textPerMonth = strings.MONTH;
    CatalogSimplePrice.defaultProps.textPerYear = strings.YEAR;
    CatalogSimplePrice.defaultProps.textOff = strings.OFF;

    Search.defaultProps.textNoResults = strings.FASTSEARCH_NO_RESULTS;
    Search.defaultProps.textSearchPlaceholder = strings.FASTSEARCH_PLACEHOLDER;
    Search.defaultProps.textShowAllResults = strings.FASTSEARCH_SHOW_ALL;
    Search.defaultProps.textSearchLabel = strings.SEARCH;
    Search.defaultProps.textClearLabel = strings.CLEAR;

    keyboardSortableDesc.onFocusDesc = strings.DRAG_ITEM_ON_FOCUS_DESC;
    keyboardSortableDesc.onGrabDescA = strings.DRAG_ITEM_ON_GRAB_DESC_A;
    keyboardSortableDesc.onGrabDescB = strings.DRAG_ITEM_ON_GRAB_DESC_B;
    keyboardSortableDesc.onDragDesc = strings.DRAG_ITEM_ON_DRAG_DESC;
    keyboardSortableDesc.onDropDesc = strings.DRAG_ITEM_ON_DROP_DESC;
    keyboardSortableDesc.onCancelDesc = strings.DRAG_ITEM_ON_CANCEL_DESC;
    keyboardSortableListItemDefaultProps.dragItemName = strings.DRAGGABLE_ITEM;

    ToolsMenu.defaultProps.current = strings.CURRENT;
}

const NeedTranslatedPrefix = 'TRANSLATE:';
export function replaceUnTranslatedDefaultProps(Component, langPack) {
    const {defaultProps} = Component;
    replaceUnTranslatedString(defaultProps, langPack);
}

export function replaceUnTranslatedString(obj, langPack) {
    _.map(obj, (value, key) => {
        if (value && _.isString(value) && value.indexOf(NeedTranslatedPrefix) === 0) {
            const resourceKey = value.replace(NeedTranslatedPrefix, '');
            const translatedString = _.get(langPack, resourceKey);

            if (!_.isEmpty(translatedString)) {
                obj[key] = translatedString;
            }
        }
    });
}
