package br.usp.ime.checkattendance.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import br.usp.ime.checkattendance.R;
import br.usp.ime.checkattendance.StudentHomeActivity;
import br.usp.ime.checkattendance.adapters.SeminarAdapter;
import br.usp.ime.checkattendance.models.Seminar;
import br.usp.ime.checkattendance.utils.ClickListener;
import br.usp.ime.checkattendance.utils.NetworkController;
import br.usp.ime.checkattendance.utils.Parser;
import br.usp.ime.checkattendance.utils.RequestQueueSingleton;
import br.usp.ime.checkattendance.utils.ServerCallback;

/**
 * Created by kanashiro on 5/6/17.
 */

public class SeminarsFragment extends Fragment {

    private ArrayList<Seminar> seminars;
    private String allSeminars;
    private String type;
    private Bundle args;
    private View rootView;
    private RecyclerView recyclerView;
    private SeminarAdapter adapter;
    private LinearLayoutManager linearLayoutManager;
    private ClickListener listener;

    public SeminarsFragment() {
        this.seminars = new ArrayList<Seminar>();
    }

    public void setListener(ClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getBundleArgs();
    }

    private void getBundleArgs() {
        this.args = getArguments();
        this.allSeminars = args.getString("response");
        this.type = args.getString("type");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        this.inflateLayout(inflater, container);

        this.seminars = Parser.parseStringResponse(this.allSeminars);

        this.adapter = new SeminarAdapter(this.seminars, this.type, this.listener);
        this.linearLayoutManager = new LinearLayoutManager(getActivity());
        this.setupRecyclerView();

        return this.rootView;
    }

    private void inflateLayout(LayoutInflater inflater, ViewGroup container) {
        if (this.type.equals("student")) {
            this.rootView = inflater.inflate(R.layout.fragment_seminars, container, false);
        } else {
            this.rootView = inflater.inflate(R.layout.fragment_seminars_teacher, container, false);
        }
    }

    private void setupRecyclerView() {
        this.recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
        this.recyclerView.setHasFixedSize(true);
        this.recyclerView.setAdapter(adapter);
        this.recyclerView.setLayoutManager(this.linearLayoutManager);
    }

    public void setData(String seminars) {
        ArrayList<Seminar> allSeminars= Parser.parseStringResponse(seminars);

        SeminarAdapter adapter = new SeminarAdapter(allSeminars, this.type, this.listener);
        this.recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

}

@@ -1287,17 +1287,16 @@
 gment %7B%0A
-%0A
     priv
@@ -2052,17 +2052,35 @@
 ing(
-%22
+getString(R.string.
 response
 %22);%0A
@@ -2075,17 +2075,17 @@
 response
-%22
+)
 );%0A     
@@ -2118,14 +2118,32 @@
 ing(
-%22
+getString(R.string.
 type
-%22
+)
 );%0A 
@@ -2295,17 +2295,16 @@
 tate) %7B%0A
-%0A
         
@@ -2407,33 +2407,32 @@
 s.allSeminars);%0A
-%0A
         this.ada
@@ -2758,21 +2758,37 @@
 als(
-%22
+getString(R.string.
 student
-%22
 ))
- %7B
+)
 %0A   
@@ -2887,17 +2887,13 @@
     
- %7D
  else
- %7B
 %0A   
@@ -2993,18 +2993,8 @@
 e);%0A
-        %7D%0A
     
@@ -3336,24 +3336,24 @@
 seminars) %7B%0A
+
         Arra
@@ -3418,17 +3418,16 @@
 inars);%0A
-%0A
         

remove hardcoded strings frmo SeminarsFragment