class DiaryController < ApplicationController

  before_action :authenticate_user!
  before_action :set_plan
  before_action :set_params, only: [:meals, :routines]
  before_action :set_routine, only: [:charge, :routines]
  before_action :set_comment, only: [:notas_alimentacion, :notas_entrenamiento, :trace_comments]

  include DiaryHelper
  
  def meals
    # Si llegan valores mayores a los mínimos máximos saltamos registro
    @meal = @plan.meals.find(@id)
    if @meal && (@value >= 0 && @value <= 3) # Value puede tener valores 0|1|2|3
      register = registro_diario(@plan) # Obtenemos el registro diario de comidas
      register[@plan.index_day][@meal.index] = @value # Registramos la comida correspondiente al día activo.
      meals_record = format_meals_record(register) # No se puede hacer registro de comidas en días anteriores o futuros
      @plan.update(meals_record: meals_record )
    else
      @message = "Comida no válida :("
    end
    respond_to { |format| format.js }
  end
  
  def routines
    if @routine && @value < 4
      @registro = registro_rutina(@routine)
      @registro[@plan.index_day] = @value
      @routine.update(history: @registro.join("|"))
    end
  end

  def record_measurements
    @plan = current_user.plan_active
    if @plan
      @plan.update(plan_params)
    else
    end
  end

  def charge
    if @routine
      params[:rw].each do |rw|
        @routine_workout = @routine.routine_workouts.find(rw[0])
        @registro = registro_carga(@routine_workout)
        @registro[@plan.index_day] = params[:rw][rw[0]]
        @routine_workout.update(history: @registro.join("|"))
      end
    end
  end

  def notas_alimentacion
    if @comment.blank?
      @message = "Escribe una nota"
    else
      registro = plan_notes(@plan, @plan.comments)
      registro[@plan.index_day] += "#{@comment.delete('|')}~"
      @plan.update(comments: registro.join("|"))
    end
    respond_to { |format| format.js }
  end

  def notas_entrenamiento
    if @comment.blank?
      @message = "Escribe un comentario"
    else
      registro = plan_notes(@plan, @plan.sessions_notes)
      registro[@plan.index_day] += "#{@comment.delete('|')}~"
      @plan.update(sessions_notes: registro.join("|"))
    end
    respond_to { |format| format.js }
  end

  def trace_comments
    if @plan.trace_comments.blank?
      @plan.update(trace_comments: @comment)
      @message = Message.create(emisor_id: current_user.id, user_id: @plan.trainer_id, message: "#{@comment} <br><a href='/plans/#{@plan.id}/edit'>15 días</a>")
    end
    @message = "Recibirás un mensaje de tu entrenador, realizaremos ajustes a tu plan en caso de que lo consideremos necesario."
  end

  private

  def plan_params
    params.require(:plan).permit(:arm_left,:arm_right,:bci,:bcd,:chest,:waist,:gluteus,:thight_left,:calf_left,:thight_right,:calf_right)
  end

  def set_routine
    @routine = @plan.routines.find_by_safe_url(params[:id])
  end

  def set_comment
    @comment = params[:comment]
  end

  def set_params
    @id = params[:id].to_i
    @value = params[:value].to_i
  end

  def set_plan
    if current_user.plan_active
      @plan = current_user.plan_active
    else
      flash[:notice] = "Aún no has contratado ningún plan"
      redirect_to "/entrenamiento"
    end
  end

end