package com.example.habittracker2017;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import static com.example.habittracker2017.UserManager.user;

/**
 * Created by hyuan2 on 2017-11-12.
 */

public class manageSharing extends Fragment {

    private static RequestAdapter<String> adapter;
    private static FollowManagerAdapter adapter1,adapter2;

    private Button addRequest;
    private EditText nameText;
    private ListView requestList,followerList,followingList;
    //boolean allowRefresh = false;


    public static manageSharing newInstance(int position) {
        manageSharing fragment = new manageSharing();
        Bundle args = new Bundle();
        args.putInt("position", position);
        fragment.setArguments(args);

        return fragment;
    }

    public manageSharing() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
           /* mPosition = getArguments().getInt("position");*/
        }

    }

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

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        addRequest = getView().findViewById(R.id.requestButton);
        nameText = getView().findViewById(R.id.requestInput);
        requestList = getView().findViewById(R.id.requestList);
        followerList = getView().findViewById(R.id.followedList);
        followingList = getView().findViewById(R.id.followingList);

        addRequest.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                //allowRefresh = true;
                String username = nameText.getText().toString();
                nameText.setText("");
                if(!username.equals("")){
                    UserManager.user.addFollowing(username);
                    UserManager.save();
                }
            }
        });

        //Get requests to this user, and adds an adapter to them.
        RemoteClient.checkRequests task = new RemoteClient.checkRequests();
        task.execute();
        try {
            adapter = new RequestAdapter<>(this.getContext(), R.layout.request_list_item, task.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        requestList.setAdapter(adapter);

        try{ArrayList<String> followers = UserManager.user.getFollowers();
        ArrayList<String> following = UserManager.user.getFollowing();

        adapter1 = new FollowManagerAdapter(followers,getActivity());
        adapter2 = new FollowManagerAdapter(following,getActivity());

        followerList.setAdapter(adapter1);
        followingList.setAdapter(adapter2);}
        catch ( Exception e){e.printStackTrace();}
    }

 /*   public void onResume() {
        super.onResume();
        if(allowRefresh) {
            allowRefresh = false;
            getFragmentManager().beginTransaction().detach(this).attach(this).commit();
        }
    }*/
}

@@ -733,16 +733,139 @@
 pter2;%0A%0A
+    private ArrayList%3CString%3E followers;%0A    private ArrayList%3CString%3E following;%0A    private ArrayList%3CString%3E requests;%0A%0A
     priv
@@ -2878,16 +2878,51 @@
   try %7B%0A
+            requests = task.get();%0A
         
@@ -3003,26 +3003,24 @@
 t_item, 
-task.get()
+requests
 );%0A     
@@ -3228,34 +3228,16 @@
     try%7B
-ArrayList%3CString%3E 
 follower
@@ -3280,34 +3280,16 @@
 %0A       
- ArrayList%3CString%3E
  followi
@@ -3614,19 +3614,18 @@
 %0A    %7D%0A%0A
+%0A
  
-/*
    publi
@@ -3682,145 +3682,527 @@
     
-if(allowRefresh) %7B%0A            allowRefresh = false;%0A            getFragmentM
+followers = UserManager.user.getFollowers();%0A        following = UserManager.user.getFollowing();%0A        adapter1.notifyDataSetChanged();%0A        adapter2.notifyDataSetChanged();%0A        RemoteClient.checkRequests task = new RemoteClient.checkRequests();%0A        task.execute();%0A        try %7B%0A            requests = task.get();%0A            adapter.notifyDataSetCh
 an
-a
 ge
-r().beginTransaction().detach(this).attach(this).commit
+d();%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D catch (ExecutionException e) %7B%0A            e.printStackTrace
 ();%0A
@@ -4216,13 +4216,11 @@
  %7D%0A    %7D
-*/
 %0A%7D%0A

Manage Others now automatically updates itself.