#!/bin/env ruby
# encoding: utf-8

class UserMailer < ActionMailer::Base

  default from: '',
          to: ''

  def new_contact_message(contact_message)
    @contact_message = contact_message

    message_info = {
      reply_to: contact_message.email,
      subject: ''
    }
    if Rails.env.development?
      message_info[:to] = ''
    end
    mail message_info
  end

  def new_information_request(information_request)

    if information_request.document_front_image.present?
      attachments[information_request.readable_document_image_filename(:front)] = File.read(information_request.document_front_image.path)
    end

    if information_request.document_back_image.present?
      attachments[information_request.readable_document_image_filename(:back)] = File.read(information_request.document_back_image.path)
    end

    @information_request = information_request
    recipient = (information_request.institution.nil? ? '' : information_request.institution.information_officer.email)

    if Rails.env.development?
      recipient = ''
    end

    mail(
      to: recipient,
      from: information_request.email,
      reply_to: information_request.email,
      subject: ''
    )
  end

  def information_request_email_confirm(information_request)
    @information_request = information_request
    recipient = information_request.email
    if Rails.env.development?
      recipient = ''
    end
    mail(
      to: recipient,
      subject: ''
    )
  end

  def new_information_request_user_notification(information_request)
    @information_request = information_request
    recipient = information_request.email
    officer_email = (information_request.institution.nil? ? nil : information_request.institution.information_officer.email)
    if Rails.env.development?
      recipient = ''
      officer_email = ''
    end
    mail(
      to: recipient,
      cc: officer_email,
      subject: ''
    )
  end

  def new_information_request_user_create(information_request)
    @information_request = information_request
    recipient = information_request.email
    if Rails.env.development?
      recipient = ''
    end
    mail(
      to: recipient,
      subject: ''
    )
  end

  def complaint_email_confirm(complaint)
    @complaint = complaint
    mail(
      to: complaint.email,
      subject: ''
    )
  end

  def new_complaint(complaint)
    @complaint = complaint
    recipient = (complaint.institution.nil? ? '' : complaint.institution.information_officer.email)

    if Rails.env.development?
      recipient = ''
    end

    mail(
      to: recipient,
      from: complaint.email,
      reply_to: complaint.email,
      subject: ''
    )
  end

  def new_information_request_comment_to_user(user, comment)
    @user = user
    @comment = comment
    @information_request = comment.commentable
    @official = InformationOfficer.where(email: comment.user_email).first
    message_info = {
      to: user.email,
      from: comment.user_email,
      reply_to: comment.user_email,
      subject: " #{comment.user_name}"
    }
    if Rails.env.development?
      message_info[:to] = ''
    end
    mail message_info
  end

  def new_information_request_comment_invite_user(user, comment)
    @user = user
    @comment = comment
    @information_request = comment.commentable
    @official = InformationOfficer.where(email: comment.user_email).first
    message_info = {
      to: user.email,
      from: comment.user_email,
      reply_to: comment.user_email,
      subject: " #{comment.user_name}"
    }
    if Rails.env.development?
      message_info[:to] = ''
    end
    mail message_info
  end

  def new_information_request_reply_to_comment(user, comment)
    @user = user
    @comment = comment
    @information_request = comment.commentable
    message_info = {
      from: comment.user_email,
      to: user.email,
      reply_to: comment.user_email,
      subject: " #{@information_request.correlative}"
    }
    if Rails.env.development?
      message_info[:to] = ''
    end
    mail message_info
  end

  def dowload_exported_file(exportation)
    @exportation = exportation
    mail(
      to: exportation.email,
      subject: ''
    )
  end

  def notice_complaint_notify(complaint, mailist)
    @complaint = complaint
    if !mailist.blank?
      mails = mailist
    else
      mails = (@complaint.institution_ids.map{|i| Institution.where(id: i).first.information_officer.email}).compact.reject{|o| o.empty?} + @complaint.alternate_officer.split(',')
    end
    message_info = {
      to: mails,
      subject: " #{@complaint.correlative}",
      from: @complaint.user_email,
      reply_to: @complaint.user_email
    }
    if Rails.env.development?
      message_info[:to] = ''
      message_info[:cc] = ''
    end

    if @complaint.attachment.present?
      attachments[@complaint.attachment_file_name] = File.read(@complaint.attachment.path)
    end

    mail message_info
  end

  def ofcia_complaint_notify(complaint)
    @complaint = complaint
    message_info = {
      to: '',
      cc: '',
      subject: '',
      from: @complaint.user_email,
      reply_to: @complaint.user_email
    }
    if Rails.env.development?
      message_info[:to] = ''
      message_info[:cc] = ''
    end
    mail message_info
  end

  def notify_new_forum_topic(forum_topic, user_emails)
    @topic = forum_topic
    message_info = {
      to: user_emails,
      subject: " #{@topic.forum_name}"
    }
    if Rails.env.development?
      message_info[:to] = ''
    end
    mail message_info
  end

  def notify_new_forum_post(forum_post, user_emails)
    @post = forum_post
    message_info = {
      to: user_emails,
      subject: "#{@post.topic_name}"
    }
    if Rails.env.development?
      message_info[:to] = ''
    end
    mail message_info
  end

  def notify_new_denouncement(complaint)
    @complaint = complaint
    if @complaint.picture.present?
      attachments['warn_picture.jpg'] = File.read(@complaint.picture.path)
    end
    #attachments['warn_picture.jpg'] = File.open("#{Rails.root}/public"+@complaint.picture.url(:mailer, timestamp: false)).path
    message_info = {
      to: [''],
      cc: '',
      subject: ""
    }
    mail message_info
  end

  def information_request_report_oir
    message_info = {
      to: user_emails,
      subject: "#{@post.topic_name}"
    }
    if Rails.env.development?
      message_info[:to] = ''
    end
    mail message_info
  end

  def information_request_report_ssta
    message_info = {
      to: [''],
      cc: [''],
      subject: "Reporte SGS #{Date.current.strftime('%d/%m/%Y')}"
    }
    if Rails.env.development?
      message_info[:to] = ''
      message_info[:cc] = ''
    end
    mail message_info
  end

  def information_request_report_capres_ssta
    message_info = {
      to: [''],
      cc: [''],
      subject: "Reporte CAPRES SGS #{Date.current.strftime('%d/%m/%Y')}"
    }
    if Rails.env.development?
      message_info[:to] = ''
      message_info[:cc] = ''
    end
    mail message_info
  end

  def information_ranking_activity(user)
    @user = user
    message_info = {
      to: user.email,
      reply_to: '',
      subject: ""
    }
    if Rails.env.development?
      message_info[:to] = ''
    end
    mail message_info
  end

  def register_participant_email(participant)
    @participant = participant
    message_info = {
      to: '',
      cc: [''],
      subject: 'Registro de participante para el diplomado virtual EFT'
    }
    if Rails.env.development?
      message_info[:to] = ''
      message_info[:cc] = ''
    end
    mail message_info
  end

  def report_schedule_events(schedule, events)
    @schedule = schedule
    @events = events
    message_info = {
      to: schedule.owner_email,
      subject: "Eventos para el día #{Date.current.strftime('%d/%m/%Y')}",
      from: ''
    }
    if Rails.env.development?
      message_info[:to] = ''
    end
    mail message_info
  end

  def api_notification(api_key)
    @api_key = api_key
    message_info = {
      to: api_key.team,
      subject: 'Api token para Gobierno Abierto',
      from: ''
    }
    attachments['uso_api_ga.pdf'] = File.read("#{Rails.root.to_s}/public/uso_api_ga.pdf")
    if Rails.env.development?
      message_info[:to] = ''
    end
    mail message_info
  end


end
