package com.johncorser.selfiesnap;

import android.app.Activity;
import android.app.AlertDialog;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseInstallation;
import com.parse.ParseObject;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import com.parse.SaveCallback;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.parse.ParseUser.getCurrentUser;

public class RecipientsActivity extends Activity {

    public static final String TAG = RecipientsActivity.class.getSimpleName();
    ParseRelation<ParseUser> mFriendsRelation;
    ParseUser mCurrentUser;
    protected List<ParseUser> mFriends;
    protected MenuItem mSendMenuItem;
    protected Uri mMediaUri;
    protected String mFileType;
    protected GridView mGridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.user_grid);
        mGridView = (GridView)findViewById(R.id.friendsGrid);
        TextView emptyTextView = (TextView)findViewById(android.R.id.empty);
        mGridView.setEmptyView(emptyTextView);

        mGridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        mGridView.setOnItemClickListener(mOnItemClickListener);
        mMediaUri = getIntent().getData();

        mFileType = getIntent().getExtras().getString("fileType");

    }

    @Override
    public void onResume(){
        super.onResume();
        mCurrentUser = getCurrentUser();
        mFriendsRelation = mCurrentUser.getRelation("friendsRelation");
        setProgressBarIndeterminateVisibility(true);
        ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
        query.addAscendingOrder("username");
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> friends, ParseException e) {
                setProgressBarIndeterminateVisibility(false);
                if (e == null){
                    mFriends = friends;

                    String[] usernames = new String[mFriends.size()];
                    int i = 0;
                    for(ParseUser user : mFriends){
                        usernames[i] = user.getUsername();
                        i++;

                    }
                    if (mGridView.getAdapter() == null) {
                        UserAdapter adapter = new UserAdapter(RecipientsActivity.this, mFriends);
                        mGridView.setAdapter(adapter);
                    }
                    else{
                        ((UserAdapter)mGridView.getAdapter()).refill(mFriends);
                    }
                }

                else{
                    Log.e(TAG, e.getMessage());

                    AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
                    builder.setMessage(e.getMessage());
                    builder.setTitle(R.string.signup_error_title);
                    builder.setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.recipients, menu);
        mSendMenuItem = menu.getItem(0);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id){
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            case R.id.action_send:
                ParseObject message = createMessage();
                if (message == null){
                    //error
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("Try again! File failed to send");
                    builder.setTitle("Sorry!");
                    builder.setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();

                }
                else{
                    send(message);
                    finish();
                }
                return true;


        }
        return super.onOptionsItemSelected(item);
    }


    protected ParseObject createMessage(){
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("RecentFace");
        query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
        String face = "";
        try {
            List<ParseObject> faces = query.find();
            for (ParseObject madeFace : faces) {
                face = madeFace.getString("faceToMake");
                madeFace.deleteInBackground();
            }

        }
        catch (ParseException e){
            Log.e(TAG, "Error: " + e);
        }


        ParseObject message = new ParseObject("Messages");
        message.put("senderId", getCurrentUser().getObjectId());
        message.put("senderName", getCurrentUser().getUsername());
        message.put("fileType", mFileType);
        message.put("recipientIds", getRecipientIds());
        message.put("face", face);
        ParseQuery<ParseObject> testQuery = new ParseQuery<ParseObject>("Streak");
        boolean firstGame = true;
        for (String recipient : getRecipientIds() ){
            try {
                List<ParseObject> streaks = testQuery.find();
                if (streaks.isEmpty()){
                    ParseObject newStreak = new ParseObject("Streak");
                    String[] players = new String[2];
                    players[0] = recipient;
                    players[1] = ParseUser.getCurrentUser().getObjectId();
                    newStreak.put("players", Arrays.asList(players));
                    newStreak.put("streak", 0);
                    newStreak.put("bestStreak", 0);
                    try {
                        newStreak.save();
                    }
                    catch (ParseException e){
                        Log.e(TAG, "Exception: " + e);
                    }
                }
                    else {
                    for (ParseObject streak : streaks) {
                        try {
                            if (streak.getJSONArray("players").getString(0).equals(ParseUser.getCurrentUser().getObjectId()) || streak.getJSONArray("players").getString(1).equals(ParseUser.getCurrentUser().getObjectId())) {
                                if (streak.getJSONArray("players").getString(0).equals(message.getString("senderId")) || streak.getJSONArray("players").getString(1).equals(message.getString("senderId"))) {
                                    firstGame = false;
                                }

                            }
                        } catch (JSONException e) {
                            Log.e("MessageAdapter", "Error in loop: " + e);
                        }
                        if (firstGame){
                            ParseObject newStreak = new ParseObject("Streak");
                            String[] players = new String[2];
                            players[0] = recipient;
                            players[1] = ParseUser.getCurrentUser().getObjectId();
                            newStreak.put("players", Arrays.asList(players));
                            newStreak.put("streak", 0);
                            newStreak.put("bestStreak", 0);
                            try {
                                newStreak.save();
                            } catch (ParseException e) {
                                Log.e(TAG, "Exception: " + e);
                            }
                        }
                    }
                }
            }
            catch (ParseException e){
                Log.e("MessageAdapter", "Error: " + e);
            }




        }

        byte[] fileBytes = FileHelper.getByteArrayFromFile(this, mMediaUri);
        if (fileBytes == null){
            return null;
        }
        else{
            if (mFileType.equals("image")){
                fileBytes = FileHelper.reduceImageForUpload(fileBytes);
            }

            String fileName = FileHelper.getFileName(this, mMediaUri, mFileType);
            ParseFile file = new ParseFile(fileName, fileBytes);
            message.put("file", file);
            return message;
        }
    }

    protected ArrayList<String> getRecipientIds(){
        ArrayList<String> recipientIds = new ArrayList<String>();
        for (int i = 0; i < mGridView.getCount(); i++ ){
            if (mGridView.isItemChecked(i)){
                recipientIds.add(mFriends.get(i).getObjectId());
            }
        }
        return recipientIds;
    }

    protected void send(ParseObject message){
        message.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null){
                    //success
                    Toast.makeText(RecipientsActivity.this, "Message sent!", Toast.LENGTH_LONG);
                    sendPush();
                }
                else{
                    AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
                    builder.setMessage("Try again! File failed to send");
                    builder.setTitle("Sorry!");
                    builder.setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }

            }
        });
    }
    protected OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            if (mGridView.getCheckedItemCount() > 0) {
                mSendMenuItem.setVisible(true);
            }
            else {
                mSendMenuItem.setVisible(false);
            }

            ImageView checkImageView = (ImageView)view.findViewById(R.id.checkImageView);

            if (mGridView.isItemChecked(position)) {
                // add the recipient
                checkImageView.setVisibility(View.VISIBLE);
            }
            else {
                // remove the recipient
                checkImageView.setVisibility(View.INVISIBLE);
            }
        }
    };
    protected void sendPush(){
        ParseQuery query = ParseInstallation.getQuery();
        query.whereContainedIn("userId", getRecipientIds());

        ParsePush push = new ParsePush();
        push.setQuery(query);
        push.setMessage(getString(R.string.new_message_notification, getCurrentUser().getUsername()));
        push.sendInBackground();
    }
}

