class SitePanelApiController < ApplicationController
  def index
    ip = request.remote_ip
    case ip
    when "211.72.229.126"
      type = "Pending"
    when "211.72.229.122"
      type = "Store"
    when "127.0.0.1"
      type = "Pending"
    else
      render :text => "Not a valid IP." and return
    end

    list = SiteConstruct.where(:server_type => type, :constructed => false)
    string = ""
    list.each do |entry|
      string = string + "#{entry.server_type},#{entry.domain_name},#{entry.port},#{entry.db_name},#{entry.path},#{entry.site_name}\n"
    end
    render :text => string
  end

  def constructed
    site_id = params[:site_id]
    ip = request.remote_ip
    case ip
    when "211.72.229.126"
    when "211.72.229.122"
    when "127.0.0.1"
    else
      render :text => "Not a valid IP." and return
    end
    site = SiteConstruct.where(:site_name => site_id, :constructed => false).first rescue nil
    if !site.nil?
      site.constructed = true
      site.save
      user = User.find(site.user_id) rescue nil
      if !user.nil?
        email = Email.new
        email.mail_to = user.member_profile.email
        email.mail_subject = "#{site.site_name} has been constructed."
        email.mail_content = "<strong>#{site.site_name}</strong> has been constructed successfully.<br />You may access the site at <a href='#{site.domain_name}'>#{site.domain_name}</a><br /><br />For any questions, please contact R&amp;D Team.<br />Thank you."
        email.deliver
      end
      render :text => "success"
    else
      render :text => "Site not found. Either it is already constructed or not queued for construction."
    end
  end
end