package cmput301w16t15.shareo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.honorato.multistatetogglebutton.MultiStateToggleButton;
import org.honorato.multistatetogglebutton.ToggleButton;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import mvc.AppUserSingleton;
import mvc.Bid;
import mvc.Thing;
import mvc.User;


/**
 * Fragment: bids main page.
 * offered and bids are the two options using the multistatetoggle button.
 */
public class BidsFragment extends Fragment implements Observer {
    private User mUser;

    private ListView mList;
    private ArrayAdapter mListAdapter;
    private TextView mEmptyMessage;
    private int mPosition = 0;


    public BidsFragment() {
        // Required empty public constructor
    }

    public static BidsFragment newInstance() {
        return new BidsFragment();
    }

    /*
    * The listview renders either the items I am bidding on, or my items other's are bidding on
    * Based on mSelectedMode this function renders the appropriate data in the listivew
    * */
    private void setAdapterBasedOnTab() {
        switch (mPosition) {
            case 0:
                new GetOffersTask().execute();
                break;
            case 1:
                new GetBidsTask().execute();
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // user singleton...used for getting data
        mUser = AppUserSingleton.getInstance().getUser();
        //mUser.addObserver(this);

        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_bids, container, false);
        mList = (ListView) v.findViewById(R.id.listview);

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                if (mPosition == 0) {
                    AcceptDeclineBidsFragment adbf = new AcceptDeclineBidsFragment();
                    final Bundle bundle = new Bundle();
                    bundle.putInt("pos", position);
                    try {
                        Thread t = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                bundle.putSerializable("myThing", (Thing) mList.getItemAtPosition(position));
                            }
                        });
                        t.start();
                        t.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    adbf.setArguments(bundle);
                    adbf.show(getFragmentManager(), "accept_decline");
                } else {
                    ViewBidFragment vgf = new ViewBidFragment();
                    final Bundle bundle = new Bundle();

                    // start new thread to avoid network on main thread.
                    try {
                        Thread t = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                bundle.putSerializable("myThing", (Bid) mList.getItemAtPosition(position));
                            }
                        });
                        t.start();
                        t.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    vgf.setArguments(bundle);
                    vgf.show(getFragmentManager(), "viewGame");
                }
            }
        });

        mEmptyMessage = (TextView) v.findViewById(R.id.empty_notice);

        MultiStateToggleButton button = (MultiStateToggleButton) v.findViewById(R.id.mstb_multi_id);
        button.setElements(R.array.bids_choices, 0);
        button.setOnValueChangedListener(new ToggleButton.OnValueChangedListener() {
            @Override
            public void onValueChanged(int position) {
                mPosition = position;
                setAdapterBasedOnTab();
            }
        });

        setAdapterBasedOnTab();

        return v;
    }

    @Override
    public void update(Observable observable, Object data) {
        mListAdapter.notifyDataSetChanged();
    }

    class GetBidsTask extends AsyncTask<String, Void, List<Bid>> {

        @Override
        protected List<Bid> doInBackground(String... params) {
            return mUser.getBids();
        }

        @Override
        protected void onPostExecute(List<Bid> d) {
            mListAdapter = new CustomAdapters.BasicBidAdapter(getActivity(), R.layout.minimal_thing_row, d);
            mList.setAdapter(mListAdapter);

            if (d.size() == 0) {
                mEmptyMessage.setVisibility(View.VISIBLE);
            } else {
                mEmptyMessage.setVisibility(View.GONE);
            }
        }
    }

    class GetOffersTask extends AsyncTask<String, Void, List<Thing>> {

        @Override
        protected List<Thing> doInBackground(String... params) {
            return mUser.getOffers();
        }

        @Override
        protected void onPostExecute(List<Thing> d) {
            mListAdapter = new CustomAdapters.BasicThingAdapter(getActivity(), R.layout.minimal_thing_row, d);
            mList.setAdapter(mListAdapter);

            if (d.size() == 0) {
                mEmptyMessage.setVisibility(View.VISIBLE);
            } else {
                mEmptyMessage.setVisibility(View.GONE);
            }
        }
    }

}

@@ -972,16 +972,42 @@
 on = 0;%0A
+    private List%3C?%3E data;%0A
 %0A%0A    pu
@@ -2566,1391 +2566,415 @@
     
-try %7B%0A                        Thread t = new Thread(new Runnable() %7B%0A                            @Override%0A                            public void run() %7B%0A                                bundle.putSerializable(%22myThing%22, (Thing) mList.getItemAtPosition(position));%0A                            %7D%0A                        %7D);%0A                        t.start();%0A                        t.join();%0A                    %7D catch (InterruptedException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A                    adbf.setArguments(bundle);%0A                    adbf.show(getFragmentManager(), %22accept_decline%22);%0A                %7D else %7B%0A                    ViewBidFragment vgf = new ViewBidFragment();%0A                    final Bundle bundle = new Bundle();%0A%0A                    // start new thread to avoid network on main thread.%0A                    try %7B%0A                        Thread t = new Thread(new Runnable() %7B%0A                            @Override%0A                            public void run() %7B%0A                                bundle.putSerializable(%22myThing%22, (Bid) mList.getItemAtPosition(position));%0A                            %7D%0A                        %7D);%0A                        t.start();%0A                        t.join();%0A                    %7D catch (InterruptedException e) %7B%0A                        e.printStackTrace();%0A                    %7D
+bundle.putSerializable(%22myThing%22, (Thing) data.get(position));%0A                    adbf.setArguments(bundle);%0A                    adbf.show(getFragmentManager(), %22accept_decline%22);%0A                %7D else %7B%0A                    ViewBidFragment vgf = new ViewBidFragment();%0A                    final Bundle bundle = new Bundle();%0A                    bundle.putSerializable(%22myThing%22, (Bid) data.get(position));
 %0A   
@@ -4064,24 +4064,46 @@
 t%3CBid%3E d) %7B%0A
+            data = d;%0A
             
@@ -4716,24 +4716,46 @@
 Thing%3E d) %7B%0A
+            data = d;%0A
             

store data in member to avoid network call