class Panel::Location::BackEnd::LocationsController < OrbitBackendController
  include AdminHelper
  open_for_visitor :only => [:get_location_categories, :get_categorywise_locations]

  def index
    @locations = LocationInfo.all
    @categories = get_categories_for_index
    @location_infos = [];
    @locations.each do |loc|
      loc['color'] = Category.find(loc.category_id).custom_value
      @location_infos << loc
    end
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @locations }
    end
  end

  def new
    @location_info = LocationInfo.new
    @categories = get_categories_for_index

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @location }
    end
  end

  def edit
    @location_info = LocationInfo.find(params[:id])
    @categories = get_categories_for_index

  end

  def show
    @location_info = LocationInfo.find(params[:id])
    respond_to do |format|
      format.html
      format.json { render json: @location }
    end
  end

  def create
    @location_info = LocationInfo.new(params[:location_info])
    @categories = get_categories_for_index
    if @location_info.save
      flash[:success] = "Success!!"
      redirect_to panel_location_back_end_locations_url
    else
      error_msg = @location_info.errors.full_messages
      render 'new'
    end
  end

  def update
    @location_info = LocationInfo.find(params[:id])
    @location_info.update_attributes(params[:location_info])
    redirect_to panel_location_back_end_locations_url
  end

  def destroy
    @location_info = LocationInfo.find(params[:id])
    @location_info.destroy
    redirect_to panel_location_back_end_locations_url

  end

  def get_locations
    location_infos = LocationInfo.all
    @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 << { 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

  def get_categories
    @data = Array.new
     @data << { name: "Department",
                link:  "http://#{request.host_with_port}"+"/panel/location/back_end/locations/get_locations?locale=en" }

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

  def get_location_categories
    check_mobile_api_openness
    location_categories = get_categories_for_index
    @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}"+"/#{panel_location_back_end_locations_get_categorywise_locations_path}"+"?category_id=#{category_id}"}
    end

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

  def get_categorywise_locations
    check_mobile_api_openness
    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
