class PartiesController < ApplicationController
  before_action :set_party, only: [:show, :edit, :update, :destroy]
  before_filter :authenticate_user!, except: [:index]

  # GET /parties[.json]
  def index
    @parties = Party.all
  end

  # GET /parties/1[.json]
  def show
  end

  # GET /parties/new
  def new
    @party = current_user.parties.build
  end

  # GET /parties/1/edit
  def edit
    @party.Location = decrypt_stuff(@party.Location)
    @party.Token = decrypt_stuff(@party.Token)
  end

  # POST /parties[.json]
  def create
    #validates :Name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
    @party = current_user.parties.build(party_params)
    @party.URL = makeurl
    @party.Location = encrypt_stuff(@party.Location)
    @party.Token = encrypt_stuff(@party.Token)
    @admin = @party.members.new(:m_user_name => current_user.user_name, :m_type => 'Admin')
    @admin.save
    respond_to do |format|
      if @party.save
        format.html { redirect_to @party, notice: 'Party was successfully created.' }
        format.json { render :show, status: :created, location: @party }
      else
        format.html { render :new, notice: 'lmao didnt work' }
        format.json { render json: @party.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /parties/1[.json]
  def update
    respond_to do |format|
      pp = party_params
      pp[:Location] = Party.encrypt_stuff(pp[:Location])
      pp[:Token] = Party.encrypt_stuff(pp[:Token])
      if @party.update(pp)
        format.html { redirect_to @party, notice: 'Party was successfully updated.' }
        format.json { render :show, status: :ok, location: @party }
      else
        format.html { render :edit }
        format.json { render json: @party.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /parties/1[.json]
  def destroy
    if @party.destroy
      @party.members.where(:party_id => @party.id).destroy_all
      @party.games.where(:party_id => @party.id).destroy_all
      @party.todo_items.where(:party_id => @party.id).destroy_all
      @party.announcements.where(:party_id => @party.id).destroy_all
      respond_to do |format|
        format.html { redirect_to parties_url, notice: 'Party was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  end

  private
  def encrypt_stuff(msg)
    if msg != nil
      @secret_key = 'this is a password'
      AESCrypt.encrypt(msg, @secret_key)
    else
      ''
    end
  end

  private
  def decrypt_stuff(msg)
    if msg != nil
      @secret_key = 'this is a password'
      AESCrypt.decrypt(msg, @secret_key)
    else
      ''
    end
  end

  private
  def getwordnikapi(type)
    require 'net/http'
    if type=='noun'
      @source='http://api.wordnik.com:80/v4/words.json/randomWord?hasDictionaryDef=false&includePartOfSpeech=noun&minCorpusCount=0&maxCorpusCount=-1&minDictionaryCount=1&maxDictionaryCount=-1&minLength=3&maxLength=5&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5'
    end
    if type=='adjective'
      @source='http://api.wordnik.com:80/v4/words.json/randomWord?hasDictionaryDef=false&includePartOfSpeech=adjective&minCorpusCount=0&maxCorpusCount=-1&minDictionaryCount=1&maxDictionaryCount=-1&minLength=3&maxLength=5&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5'
    end
    if type=='adverb'
      @source='http://api.wordnik.com:80/v4/words.json/randomWord?hasDictionaryDef=false&includePartOfSpeech=adverb&minCorpusCount=0&maxCorpusCount=-1&minDictionaryCount=1&maxDictionaryCount=-1&minLength=3&maxLength=-1&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5'
    end
    JSON.parse(Net::HTTP.get_response(URI.parse(@source)).body).to_a[1][1]
  end

  private
  def makeurl
    getwordnikapi('adverb').capitalize+getwordnikapi('adjective').capitalize+getwordnikapi('noun').capitalize
  end

  # Use callbacks to share common setup or constraints between actions.
  private
  def set_party
    @party = Party.friendly.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  private
  def party_params
    params.require(:party).permit(:Name, :Date, :Location, :Requirements, :Password, :URL, :Token)
  end
end