package org.deke.risk.riskahead.fragment;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import org.deke.risk.riskahead.ProfileActivity;
import org.deke.risk.riskahead.R;
import org.deke.risk.riskahead.helper.AppConfig;
import org.deke.risk.riskahead.helper.AppController;
import org.deke.risk.riskahead.helper.SessionManager;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Dennis on 13.12.2015.
 */
public class ProfileStatisticsFragment extends Fragment {

    private final static String mActivityTitle = "User Statistics";
    private final static String TAG = ProfileStatisticsFragment.class.getSimpleName();

    private View view;
    private ProfileActivity parent;

    TextView points ;
    TextView ranking ;
    TextView posts ;
    TextView txtMemberSince ;

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

        points =  (TextView) view.findViewById(R.id.txt_profile_points);
        ranking =  (TextView) view.findViewById(R.id.txt_profile_ranking);
        posts =  (TextView) view.findViewById(R.id.txt_profile_numberposts);
        txtMemberSince = (TextView) view.findViewById(R.id.txt_profile_membersince);

        getProfileStatsForList();

        return view;
    }

    private void getProfileStatsForList() {
        StringRequest strReq = getStringRequestProfileStats();
        String tag_string_req = "req_profilestats";
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    private StringRequest getStringRequestProfileStats() {
        return new StringRequest(Request.Method.POST, AppConfig.URL_DEFAULT, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "User stats: " + response);

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    if (!error) {


                        jObj = jObj.getJSONObject("msg");
                        points.setText(jObj.getString("points"));
                        ranking.setText(jObj.getString("rank"));
                        posts.setText(jObj.getString("numberOfPosts"));
                        txtMemberSince.setText(parent.user.get("created_at"));
                    } else {
                        String errorMsg = jObj.getString("error_msg");
                        Log.e(TAG, "Error getting user stats (server returned error): " + errorMsg);
                        parent.showMessage(errorMsg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Error getting user stats: " + error.getMessage());
                parent.showMessage(getString(R.string.errormsg_couldnotretrieve));
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("tag", "getuserstats");
                params.put("uid", parent.user.get(SessionManager.KEY_UID));
                params.put("token", parent.user.get(SessionManager.TOKEN));

                return params;
            }
        };
    }

}
