class ApiController < ApplicationController

  def index
  end

  def get_albums
  	albums = Album.all
    output = Array.new

    albums.each do |album|
      if album.cover_path
        cover_path = album.cover_path
      else
        cover_path = "/assets/gallery/default.jpg"
      end
      output << {
        album_cover_file: "http://#{request.host_with_port}"+cover_path,
        album_name: album.name,
        album_link:"http://#{request.host_with_port}#{album_images_api_index_path(album.id)}",
      }
    end

    render :json=>JSON.pretty_generate(output)
  end


  def get_images
    album = Album.find(params[:id])
    images = album.album_images.all
    output = Array.new

    images.each do |image|
      if image.file.theater.present?
        @image_file = image.file.theater.url
      else
        @image_file = image.file.url
      end

      output << {
        image_title: image.title,
        image_description: image.description,
        image_file: { url: "http://#{request.host_with_port}#{@image_file}",
                      thumb: "http://#{request.host_with_port}#{image.file.thumb.to_s}"}}
    end

    render :json=>JSON.pretty_generate(output)
  end

  def get_location_categories
  	location_module = ModuleApp.where(key: "location").first
    location_categories = location_module.categories.all
    @data = Array.new

    location_categories.each do |category|
    I18n.locale = :en
    name_en = category.title
    I18n.locale = :zh_tw
    name_zh_tw = category.title
    category_id = category.id.to_s
        @data << { name_en: name_en, name_zh_tw: name_zh_tw, category_id: category_id, location_link: "http://#{request.host_with_port}"+"#{get_locations_api_index_path}"+"?category_id=#{category_id}"}
    end

    render :json => JSON.pretty_generate(@data)
  end

  def get_locations
    location_infos = LocationInfo.where(:category_id => params[:category_id])
    @data = Array.new

    location_infos.each do |location|

      picurl = location.file.blank? ? '' : "http://#{request.host_with_port + location.file.url}"
      thumburl = location.file.blank? ? '' : "http://#{request.host_with_port + location.file.thumb.url}"

      @data << { id: location.id.to_s,
                 name: location.name,
                 pic_url: picurl,
                 thumb_url: thumburl,
                 longitude: location.longitude,
                 latitude: location.latitude,
                 description: location.description }
    end

    render :json => JSON.pretty_generate(@data)
  end

end
