package com.maxmoney.maxcddconsole.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.maxmoney.maxcddconsole.Adapter.ApproveUserAdapter;
import com.maxmoney.maxcddconsole.Adapter.PaySlipAdapter;
import com.maxmoney.maxcddconsole.Class.PaySlipClass;
import com.maxmoney.maxcddconsole.Common.StandardProgressDialog;
import com.maxmoney.maxcddconsole.Connectivity.BasedUrl;
import com.maxmoney.maxcddconsole.R;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.android.volley.Request.Method.GET;

public class PaySlipActivity extends AppCompatActivity implements View.OnClickListener {

    String session;
    StandardProgressDialog standardProgressDialog;

    TextView textView_title;
    ImageView imageView_back;

    private PaySlipAdapter mPLAdapter;
    RecyclerView list_view;
    private List<PaySlipClass> menuList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_slip);

        standardProgressDialog = new StandardProgressDialog(this.getWindow().getContext());
        session = getIntent().getStringExtra("session");

        textView_title = findViewById(R.id.textView_title);
        imageView_back = findViewById(R.id.imageView_back);
        list_view = findViewById(R.id.list_view);
        imageView_back.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        standardProgressDialog.show();
        getDetails();
    }

    private void getDetails() {
        menuList = new ArrayList<>();
        list_view.setNestedScrollingEnabled(false);
        list_view.setHasFixedSize(false);
        mPLAdapter = new PaySlipAdapter(menuList);

        LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(PaySlipActivity.this, LinearLayoutManager.HORIZONTAL, false);
        list_view.setLayoutManager(horizontalLayoutManager);
        list_view.setAdapter(mPLAdapter);

        StringRequest stringRequest = new StringRequest(GET, BasedUrl.URL_STAGING +"pay-roll/my-pay-history",
                new com.android.volley.Response.Listener<String>() {
                    @Override
                    public void onResponse(final String response) {
                        standardProgressDialog.dismiss();
                        try {
                            JSONArray obj = new JSONArray(response);
                            for(int i =0; i <obj.length();i++){
                                JSONObject objs = obj.getJSONObject(i);

                                PaySlipClass menuClass = new PaySlipClass(
                                        objs.getString("createdAt"),
                                        objs.getString("branch"),
                                        objs.getString("corridor"),
                                        objs.getString("overTimeInsideBranch"),
                                        objs.getString("overTimeOutsideBranch"),
                                        objs.getString("branchIncentive"),
                                        objs.getString("corridorIncentive"),
                                        objs.getString("individualIncentive"),
                                        objs.getString("allowance"),
                                        objs.getString("claims"),
                                        objs.getString("deduction"),
                                        objs.getString("basicPay"),
                                        objs.getString("netPay")
                                );
                                menuList.add(menuClass);

                            }
                            mPLAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new com.android.volley.Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        standardProgressDialog.dismiss();

                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap();
                headers.put("api-key",session);
                return headers;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(stringRequest);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case  R.id.imageView_back: {
                onBackPressed();
                break;
            }
        }
    }
}
