# frozen_string_literal: true

class OpponentController < ApplicationController
  include SessionsHelper
  before_action :logged_in_user
  protect_from_forgery with: :exception

  def show
    team_id = params[:team_id]
    opponent_id = params[:opponent_id]
    team = TeamSnap::Team.find(@teamsnap_client, team_id)
    opponent = TeamSnap::Opponent.find(@teamsnap_client, opponent_id)
    team_media = TeamSnap::TeamMedium.search(@teamsnap_client, {team_id: team_id})
    team_media_groups = TeamSnap::TeamMedium.search(@teamsnap_client, {team_id: team_id})
    opponent_logo = team_media.find{|tm| tm.description == "opponent-logo-#{opponent_id}.png"}
    render 'show', locals: { team: team, opponent: opponent, opponent_logo: opponent_logo }
  end

  def list
    team_id = params[:team_id]
    team = TeamSnap::Team.find(@teamsnap_client, team_id)
    opponents = TeamSnap::Opponent.search(@teamsnap_client, { team_id: })
    render 'list', locals: { team:, opponents: }
  end
  
  def upload_logo_get
    team_id = params[:team_id]
    opponent_id = params[:opponent_id]
    member_id = @user_id
    render 'upload_logo', locals: { team_id:, opponent_id:, member_id: }
  end
    
  
  def upload_logo_post
    team_id = params[:team_id]
    opponent_id = params[:opponent_id]
    member_results = TeamSnap::Member.search(@teamsnap_client, {user_id: @user_id, team_id: team_id})
    member_id = member_results[0].id
    file = params['file']
    
    
    new_team_medium = TeamSnap::TeamMedium.upload_team_medium(@teamsnap_client,         
      {:media_format=> "file",
      :file=> file.open(),
        :member_id=> member_id,
        :team_id=> team_id,
        :team_media_group_id=> "4927028",
        :description=> "team-logo-#{"test"}.png",position:0}
      )
      
    render plain: "Posted."
    end
    

end
