package com.mooo.ziggypop.candconline;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

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

/**
 * Created by ziggypop on 3/28/15.
 */
public class Game {
    private String gameTitle;
    private String lobbySlots;
    private ArrayList<String> players;
    private boolean isLocked;
    private String map;


    public Game(String title, String slots, ArrayList<String> players,
                boolean isLocked, String map) {
        gameTitle = title;
        lobbySlots = slots;
        this.map = map;
        this.players = players;
        this.isLocked = isLocked;
    }

    public String getTitle(){
        return gameTitle;
    }
    public String getSlots() {
        return lobbySlots;
    }
    public String getMap(){
        return map;
    }
    public ArrayList<String> getPlayersArray(){
        return players;
    }
    public String getPlayersFormat(){
        String retVal = "";
        for(String element: players) {
            retVal += element +"\n";
        }
        retVal = retVal.substring(0, retVal.length()-2);
        return retVal;
    }
    public boolean getLockStatus(){
        return isLocked;
    }










    public static class GamesAdapter extends ArrayAdapter<Game> {
        public GamesAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        public GamesAdapter( Context context, int resource, ArrayList<Game> games) {
            super(context, resource, games);
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v = convertView;

            if (v == null) {

                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.games_layout, null);

            }

            Game p = getItem(position);

            if (p != null) {

                TextView tt = (TextView) v.findViewById(R.id.game_title);
                TextView tt1 = (TextView) v.findViewById(R.id.slots);
                ImageView tt3 = (ImageView) v.findViewById(R.id.lock);

                TextView tt4 = (TextView) v.findViewById(R.id.players);
                TextView tt2 = (TextView) v.findViewById(R.id.map);

                if (tt != null) {
                    tt.setText(p.getTitle());
                }
                if (tt1 != null) {
                    tt1.setText(p.getSlots());
                }
                if (tt2 != null) {
                    tt2.setText(p.getMap());
                }
                if (tt3 != null) {
                    if (p.getLockStatus()){
                        tt3.setVisibility(View.VISIBLE);
                    } else {
                        tt3.setVisibility(View.GONE);
                    }
                }
                if (tt4 != null) {
                    tt4.setText(p.getPlayersFormat());
                }
            }

            return v;

        }
    }



    public static class GamesFragment extends ListFragment {


        private List myList;
        private GamesAdapter mAdapter;
        private static ArrayList<Game> games = new ArrayList<>();




        public GamesFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);

            mAdapter = new GamesAdapter(getActivity(), R.layout.games_layout, games);
            setListAdapter(mAdapter);


            return rootView;
        }

        public void refreshData(final ArrayList<Game> data){
            if (isAdded()) {
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        Log.v("GAMES_IN_LOBBY", "RUNNING");
                        mAdapter.clear();
                        games.addAll(data);
                        mAdapter.notifyDataSetChanged();
                    }


                });

            }
        }

    }



    public static class GamesInProgressFragment extends ListFragment {

        private GamesAdapter mAdapter;
        private static ArrayList<Game> games = new ArrayList<>();


        public GamesInProgressFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);

            mAdapter = new GamesAdapter(getActivity(), R.layout.games_layout, games);
            setListAdapter(mAdapter);

            return rootView;
        }

        /*
         * Refreshes the data used in this "tab" fragment.
         */
        public void refreshData(final ArrayList<Game> data, Activity activity){
            // This is a hacky way of avoiding a crash caused by this fragment not being added
            // fast enough- calling the getActivity() method will return null.
            // So this method takes an activity as well, in order to create a new adapter if
            // this hasn't attached yet.
            if(!isAdded()){
                mAdapter = new GamesAdapter(activity, R.layout.games_layout, games);
            }
            Log.v("GamesInProgressFragment", "RUNNING");
            mAdapter.clear();
            games.addAll(data);
            mAdapter.notifyDataSetChanged();

        }

    }




}

@@ -1098,32 +1098,111 @@
 turn map;%0A    %7D%0A
+    //TODO Adapt these into a 2xn/2 arrangement instead of inserting newlines.%0A
     public Array
@@ -2344,17 +2344,28 @@
 extView 
-t
+gameTitleTex
 t = (Tex
@@ -2429,19 +2429,25 @@
 extView 
-tt1
+slotsText
  = (Text
@@ -2506,19 +2506,25 @@
 ageView 
-tt3
+lockImage
  = (Imag
@@ -2583,19 +2583,27 @@
 extView 
-tt4
+playersText
  = (Text
@@ -2663,19 +2663,27 @@
 extView 
-tt2
+mapNameText
  = (Text
@@ -2735,17 +2735,28 @@
     if (
-t
+gameTitleTex
 t != nul
@@ -2780,17 +2780,28 @@
         
-t
+gameTitleTex
 t.setTex
@@ -2855,19 +2855,25 @@
     if (
-tt1
+slotsText
  != null
@@ -2900,11 +2900,17 @@
     
-tt1
+slotsText
 .set
@@ -2967,19 +2967,27 @@
     if (
-tt2
+mapNameText
  != null
@@ -3014,11 +3014,19 @@
     
-tt2
+mapNameText
 .set
@@ -3081,19 +3081,25 @@
     if (
-tt3
+lockImage
  != null
@@ -3162,35 +3162,41 @@
                 
-tt3
+lockImage
 .setVisibility(V
@@ -3266,11 +3266,17 @@
     
-tt3
+lockImage
 .set
@@ -3358,19 +3358,27 @@
     if (
-tt4
+playersText
  != null
@@ -3405,11 +3405,19 @@
     
-tt4
+playersText
 .set
@@ -3586,37 +3586,8 @@
 %7B%0A%0A%0A
-        private List myList;%0A
     

Clean up Game some more