(function( $ ) {
	'use strict';

	function AysSurveyChatModeDisplay(element, options){
        this.el = element;
        this.$el = $(element);
        this.htmlClassPrefix = 'ays-survey-';
        this.dbOptionsPrefix = 'survey_';
        this.ajaxAction = 'ays_survey_ajax';
        this.dbOptions = undefined;
        this.QuizQuestionsOptions = undefined;
        this.uniqueId;
        this.surveyId;

        // new added email pattern
        this.emailValivatePattern = /^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.\w{2,}$/;

        // phone pattern
        this.phoneValivatePattern = /^[0-9)(+ -]+$/;

        this.init();

        return this;
    }

	AysSurveyChatModeDisplay.prototype.init = function() {
        var _this = this;
        _this.uniqueId = _this.$el.data('id');

        if( typeof window.aysSurveyOptions != 'undefined' ){
            _this.dbOptions = JSON.parse( atob( window.aysSurveyOptions[ _this.uniqueId ] ) );
        }

        _this.setup();
		
        _this.setEvents();
    };

	AysSurveyChatModeDisplay.prototype.setup = function(e) {
        var _this = this;
        _this.chatContainers = _this.$el.find('.' + _this.htmlClassPrefix + 'chat-container');
        _this.chatContainers.find('.ays-survey-chat-question-box').first().find('.ays-survey-chat-question-item').addClass('active-chat-question');
        _this.chatContainers.find('.ays-survey-chat-answer-box').first().addClass('active-chat-answer');
    }

	AysSurveyChatModeDisplay.prototype.setEvents = function(e) {
        var _this = this;

        _this.$el.on('click', '.' + _this.htmlClassPrefix + 'chat-answer-label-content', function(e){           
			_this.nextAction($(this));
        });

		_this.$el.on('click', '.' + _this.htmlClassPrefix + 'chat-answer-input-content', function(e){ 
			_this.nextActionTextTypes($(this));
		});
    }

	AysSurveyChatModeDisplay.prototype.nextAction = function(button) {
		var $this = button;

		var clickedAnswer = $this.find('span').text();

		var questionId = $this.parents('.ays-survey-chat-answer-box').attr('data-questionid');

		var questionBox = $this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header').find('.ays-survey-chat-question-box[data-id='+questionId+']');

		var questionToGo = $this.attr('data-go-to-question') ? $this.attr('data-go-to-question') : '';

		var nextQuestion = questionBox.next();
		var nextAnswerBox = $this.parents('.ays-survey-chat-answer-box').next();
		
		if (questionToGo !== '' && questionToGo != 'submit_form') {
			nextQuestion = $this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header').find('.ays-survey-chat-question-box[data-id='+questionToGo+']');
			nextAnswerBox = $this.parents('.ays-survey-chat-content').find('.ays-survey-chat-footer').find('.ays-survey-chat-answer-box[data-questionid='+questionToGo+']');				
		}
		
		if (questionBox.index() > nextQuestion.index()) {
			nextQuestion = questionBox.next();
			nextAnswerBox = $this.parents('.ays-survey-chat-answer-box').next();
		}

		if(questionToGo != 'submit_form'){
			setTimeout(function(){ 
				questionBox.next().find('.ays-survey-chat-question-animation-dots').addClass('active-chat-question');
				$this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header-main').scrollTop( $this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header-main').height() );
			}, 100);
		}
			
		questionBox.find('.ays-survey-chat-question-reply').css({'display':'flex'});
		questionBox.find('.ays-survey-chat-question-reply-title').html('<span class="ays-survey-chat-question-title-content">'+clickedAnswer+'</span>');

		$this.parents('.ays-survey-chat-answer-box').removeClass('active-chat-answer');

		if(questionToGo != 'submit_form'){
			setTimeout(function(){ 
				questionBox.next().find('.ays-survey-chat-question-animation-dots').removeClass('active-chat-question');
				nextQuestion.find('.ays-survey-chat-question-item').addClass('active-chat-question');
				nextAnswerBox.addClass('active-chat-answer');
				$this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header-main').scrollTop( $this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header-main').height() );
				nextAnswerBox.find('.ays-survey-chat-short-text-input').focus();
			}, 2000);
		}


		if($this.parents('.ays-survey-chat-answer-box').is(':last-child')	|| questionToGo == 'submit_form'){
			setTimeout(function(){ 
				$this.parents('.ays-survey-chat-content').find('.ays-survey-chat-button-container').find('.ays-survey-finish-button').trigger('click');
				var dots = '<div class="ays-survey-chat-question-animation-dots ays-survey-chat-final-message-loader">'
				+ '<div class="ays-survey-chat-question-pre-icon"><img src="'+aysSurveyChat.questionPrIconURL+'" ></div>'
				+ aysSurveyChat.chatLoader 
				+ '</div>';
				$this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header-main').append(dots);
				$this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header').find('.ays-survey-chat-final-message-loader').addClass('active-chat-loader');
			}, 1);
		}
	}

	AysSurveyChatModeDisplay.prototype.nextActionTextTypes = function(button) {
		var $this = button;

		var clickedAnswer = $this.parents('.ays-survey-chat-answer-text-type').find('input.ays-survey-chat-short-text-input').val();

		var questionId = $this.parents('.ays-survey-chat-answer-box').attr('data-questionid');

		var questionBox = $this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header').find('.ays-survey-chat-question-box[data-id='+questionId+']');

		setTimeout(function(){ 
			questionBox.next().find('.ays-survey-chat-question-animation-dots').addClass('active-chat-question');
			$this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header-main').scrollTop( $this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header-main').height() );
		}, 100);
	
		questionBox.find('.ays-survey-chat-question-reply').css({'display':'flex'});
		questionBox.find('.ays-survey-chat-question-reply-title').html('<span class="ays-survey-chat-question-title-content">'+clickedAnswer+'</span>');

		$this.parents('.ays-survey-chat-answer-box').removeClass('active-chat-answer');
		

		setTimeout(function(){ 
			questionBox.next().find('.ays-survey-chat-question-animation-dots').removeClass('active-chat-question');
			questionBox.next().find('.ays-survey-chat-question-item').addClass('active-chat-question');
			$this.parents('.ays-survey-chat-answer-box').next().addClass('active-chat-answer');
			$this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header-main').scrollTop( $this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header-main').height() );
			$this.parents('.ays-survey-chat-answer-box').next().find('.ays-survey-chat-short-text-input').focus();
		}, 2000);


		if($this.parents('.ays-survey-chat-answer-box').is(':last-child')	){
			setTimeout(function(){ 
				$this.parents('.ays-survey-chat-content').find('.ays-survey-chat-button-container').find('.ays-survey-finish-button').trigger('click');
				var dots = '<div class="ays-survey-chat-question-animation-dots ays-survey-chat-final-message-loader"> '
				+ '<div class="ays-survey-chat-question-pre-icon"><img src="'+aysSurveyChat.questionPrIconURL+'" ></div>'
				+ aysSurveyChat.chatLoader 
				+ '</div>';
				$this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header-main').append(dots);
				$this.parents('.ays-survey-chat-content').find('.ays-survey-chat-header').find('.ays-survey-chat-final-message-loader').addClass('active-chat-loader');
				
		}, 1);
			
		}
	}


    $(document).ready(function () {
		
		

		$(document).on('keydown', '.ays-survey-chat-answer-text-type', function(event){

    		if (event.keyCode === 13) {
				var sendButton = $(this).find(".ays-survey-chat-answer-input-content");
				sendButton.trigger("click");
				event.preventDefault();
			}
    	});

		$(document).find('.ays-survey-container').AysSurveyMakerChatMode();

	});

	$.fn.AysSurveyMakerChatMode = function(options) {
        return this.each(function() {
            if (!$.data(this, 'AysSurveyMakerChatMode')) {
                $.data(this, 'AysSurveyMakerChatMode', new AysSurveyChatModeDisplay(this, options));
            } else {
                try {
                    $(this).data('AysSurveyMakerChatMode').init();
                } catch (err) {
                    console.error('AysSurveyMakerChatMode has not initiated properly');
                }
            }
        });
    };
	

})( jQuery );
