define([
  'core/js/adapt',
  'core/js/views/componentView',
  'core/js/models/componentModel'
], function(Adapt, ComponentView, ComponentModel) {

   

  var TextViewRadioOptions = ComponentView.extend({


    events: {
      'click .mr_submit_button': 'onPinClicked',
      'click .btn1' :'onConfirmClick',
      'click .btn2' :'onConfirmsClick',
      'click .closeBtn' :'hidepopUp',
      'click .shadow_bg' :'hidepopUp',
      'click .select_button' :'onConfirmClick',   
      'change input':'optioClicked'  
      
         
    },

    optioClicked:function(e)
    {
      // ;
      var length = $(e.currentTarget).closest(".container-title_survey").find(".textrd_question01 ").length
      $(e.currentTarget).parent().parent().addClass("selected")
      if($(e.currentTarget).closest(".container-title_survey").find(".selected").length>=length)
      {
        $(e.currentTarget).closest(".container-title_survey").find(".mr_submit_button").removeClass("disablebuttonSubmit");
      }
    },

    hidepopUp: function(e){         
      $(e.currentTarget).closest(".staticPageWrapper").find('.showDive').fadeOut("fast"); 
   },
    onConfirmClick: function(e){
         

         $(e.currentTarget).closest(".staticPageWrapper").find('.showDive').fadeIn("fast");
         $(e.currentTarget).addClass("disabledbtn");   
    },
    onConfirmsClick: function(){      
      $('#shohide').show();      
      },
    onPinClicked: function(e){ 
      
      this.$el.find(".showDive").show()  ;   
      // var url = $(e.currentTarget).attr("data-url")
      // window.open(url, '_blank');
      $(e.currentTarget).addClass("disablebuttonSubmit");
      $(e.currentTarget).closest(".container-title_survey").find(".lab_container").addClass("disabledoption")
    },    
    preRender: function() {    
      $(".btn1").hide();  
      var extUrl = this.model.get('_parent').get('_parent').get('extranalUrl');
      if(extUrl) this.model.set('extranalUrl',extUrl);     
      this.checkIfResetOnRevisit();

      // var script = document.createElement('script');
      // script.type = 'text/javascript';
      // script.async = true;
      // script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.min.js';
      // document.querySelector('head').appendChild(script);

      // $('.select_button').on('click',pdf)
      // $('#user_textare').on('keyup',function(){
       
      //   if($(this).val() != ''){
      //     $('.select_button').removeClass('disabledbtn');
      //   }else{
      //     $('.select_button').addClass('disabledbtn');
      //   }
      //   //$(this).val()
      //   //$('.select_bg_popup').show();
      // })

    },
    postRender: function() {
      this.setReadyStatus();
      this.setupInview();

      

      if(this.model.get('isIntro')){
        var charLen = this.$('.text__title-inner').text().trim().length
        var winWidth = $('html').width();
        if(winWidth>800){          
          var setFont= ((winWidth/charLen)*1.15).toFixed(2);
          setFont = setFont>66?66:setFont<48?48:setFont;
          this.$('.text__title-inner').closest('.component__title').attr('style','font-size:'+setFont+'px !important')
        }
        
      }

      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.async = true;
      script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.min.js';
      document.querySelector('head').appendChild(script);

     // $('.select_button').on('click',pdf)
      this.$('.user_textare').on('keyup',function(e){
       
        if($(this).val() != ''){
          $(e.currentTarget).closest(".staticPageWrapper").find('.select_button').removeClass('disabledbtn');
        }else{
          $(e.currentTarget).closest(".staticPageWrapper").find('.select_button').addClass('disabledbtn');
        }
        //$(this).val()
        //$('.select_bg_popup').show();
      })
      
    },
    setupInview: function() {     
      console.log(this.model.get('_id'))
      var selector = this.getInviewElementSelector();
      if (!selector) {       
        this.setCompletionStatus();
        return;
      }

      this.setupInviewCompletion(selector);
    },

    /**
     * determines which element should be used for inview logic - body, instruction or title - and returns the selector for that element
     */
    getInviewElementSelector: function() {
      if (this.model.get('body')) return '.component__body';

      if (this.model.get('instruction')) return '.component__instruction';

      if (this.model.get('displayTitle')) return '.component__title';

      return null; 
    },

    checkIfResetOnRevisit: function() {
      var isResetOnRevisit = this.model.get('_isResetOnRevisit');      
      // If reset is enabled set defaults
      if (isResetOnRevisit) {
        this.model.reset(isResetOnRevisit);
       
      }
    }
, 
    pdf: function(e) {
      $(e.currentTarget).closest(".staticPageWrapper").find(".btn1").show()
      $(e.currentTarget).closest(".staticPageWrapper").find(".select_button").addClass('disabledbtn');
      $(e.currentTarget).closest(".staticPageWrapper").find(".user_textare").addClass('disabledtxtArea'); 
      /*PDF ccreate code */
      // var pdf = new jsPDF();
      // pdf.setFont("helvetica");
      // pdf.setFontSize(16);
      
      // var doc =$(e.currentTarget).closest('.block').attr("data-adapt-id")
      // var splitTitle = pdf.splitTextToSize( $(e.currentTarget).closest(".staticPageWrapper").find('.user_textare').val(), 155);
      // pdf.text(15, 20, splitTitle);
      // pdf.save(doc+'_Reflection​.pdf')
/*PDF ccreate code */

      //$(this).removeClass('disabledbtn');
      //event.stopImmediatePropagation();
    }
  },
  

  {
    template: 'textRadioOptions'
  });

  

  function pdf(event){
    //if(oneFlag == false){
      // $(".btn1").show()
      // $(this).addClass('diablebutton');
      // var pdf = new jsPDF();
      // pdf.setFont("helvetica");
      // pdf.setFontSize(16);
      // var splitTitle = pdf.splitTextToSize($('#user_textare').val(), 155);
      // pdf.text(15, 20, splitTitle);
      // pdf.save('Reflection​.pdf')
      // $(this).removeClass('diablebutton');
      // event.stopImmediatePropagation();
    //}
  }

  return Adapt.register('textRadioOptions', {  
    model: ComponentModel.extend({}),// create a new class in the inheritance chain so it can be extended per component type if necessary later
    view: TextViewRadioOptions
  });
});
