package csu.bryanreilly.partypush.Network.Transactions;

import android.os.AsyncTask;
import android.util.Log;
import com.amazonaws.services.dynamodbv2.model.GetItemResult;
import com.google.android.gms.maps.model.LatLng;

import java.util.ArrayList;
import csu.bryanreilly.partypush.Network.AmazonDDB.GetDatabaseItem;
import csu.bryanreilly.partypush.Program.Constants;
import csu.bryanreilly.partypush.UserData.AccountManager;
import csu.bryanreilly.partypush.UserData.Friend.Friend;
import csu.bryanreilly.partypush.UserData.Party.Party;

public class GetParties extends AsyncTask<Void, Void, ArrayList<Party>> {
    @Override
    protected ArrayList<Party> doInBackground(Void... params) {
        ArrayList<GetItemResult> queryResults = getFriendInfo();
        ArrayList<String> partyList = getPartyIDs(queryResults);
        ArrayList<Party> partyObjects = getPartyInfo(partyList);
        return partyObjects;
    }

    private ArrayList<GetItemResult> getFriendInfo(){
        ArrayList<GetItemResult> queryResults = new ArrayList<GetItemResult>();
        for (Friend friend : AccountManager.getAddedFriends()) {
            String userID = friend.getId();
            if (userID.trim() == "" || userID == null)
                continue; // Skip blank ID's generated by EMPTY friends objects
            // Only add ID's of friends who have accepted a friend request
            if(friend.getCurrentStatus() == Friend.STATUS.FRIEND){
                queryResults.add(getUserInfo(userID));
            }
        }
        queryResults.add(getUserInfo(AccountManager.getId()));

        return queryResults;
    }

    private GetItemResult getUserInfo(String userID){
        GetDatabaseItem getUserInfo = new GetDatabaseItem(userID, Constants.USER_DATABASE, Constants.USER_DATABASE_ID);
        getUserInfo.startTransaction();
        while (!getUserInfo.isComplete()) {
            //Wait until it gets the information from the database
        }
        return getUserInfo.getResult();
    }

    private ArrayList<String> getPartyIDs(ArrayList<GetItemResult> queryResults){
        ArrayList<String> partyIDs = new ArrayList<String>();
        for(GetItemResult result: queryResults){
            if(!result.getItem().containsKey(Constants.USER_DATABASE_PARTIES))
                continue; //User has no parties
            String partiesList = result.getItem().get(Constants.USER_DATABASE_PARTIES).getS();
            String[] partiesArray = partiesList.split(",");
            //Add each individual party id to the list (input is each party separated by ",")
            for (String party: partiesArray)
                partyIDs.add(party.trim());
        }
        return partyIDs;
    }

    private ArrayList<Party> getPartyInfo(ArrayList<String> partyIDs){
        ArrayList<Party> parties = new ArrayList<Party>();
        for(String partyID : partyIDs){
            if (partyID.trim().equals("") || partyID.trim().equals("EMPTY"))
                continue; // Skip blank ID's
            GetDatabaseItem getParty = new GetDatabaseItem(partyID.trim(), Constants.PARTY_DATABASE, Constants.PARTY_DATABASE_ID);
            Log.i("GDI", "Getting Party Info " + partyID);
            getParty.startTransaction();
            while(!getParty.isComplete()){
                // Wait for database to getString information
            }

            String partyLocation = getParty.getResult().getItem().get(Constants.PARTY_DATABASE_LOCATION).getS();
            String partyName = getParty.getResult().getItem().get(Constants.PARTY_DATABASE_NAME).getS();
            String partyLatitude = getParty.getResult().getItem().get(Constants.PARTY_DATABASE_LATITUDE).getS();
            String partyLongitude = getParty.getResult().getItem().get(Constants.PARTY_DATABASE_LONGITUDE).getS();
            LatLng partyCoordinates = new LatLng(Double.parseDouble(partyLatitude), Double.parseDouble(partyLongitude));
            Log.i("PARTIES", partyName + " " + partyLocation);
            Party party = new Party(partyName, partyLocation, partyID, partyCoordinates);
            parties.add(party);
        }
        return parties;
    }

    @Override
    protected void onPostExecute(ArrayList<Party> parties) {
        AccountManager.setParties(parties);
    }
}