package com.maxmoney.maxcddconsole.Activity;

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.TextView;

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.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,textView_next,textView_next2;
    ImageView imageView_back;

    private PaySlipAdapter mPLAdapter;
    RecyclerView list_view;
    private List<PaySlipClass> menuList;

    int total;
    @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);
        textView_next = findViewById(R.id.textView_next);
        textView_next2 = findViewById(R.id.textView_next2);
        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);

        final 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();


                            total = mPLAdapter.getItemCount();


                            list_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
                                @Override
                                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                                    super.onScrollStateChanged(recyclerView, newState);
                                    if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                                        //Dragging
                                    } else if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                                        int review_position = horizontalLayoutManager.findFirstVisibleItemPosition();



                                    }
                                }


                                @Override
                                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                                    super.onScrolled(recyclerView, dx, dy);

                                    final int position = horizontalLayoutManager.findFirstVisibleItemPosition();


                                    if(position == (total-1)){
                                        textView_next2.setVisibility(View.VISIBLE);
                                        textView_next.setVisibility(View.GONE);
                                    }else if(position == 0){

                                        textView_next2.setVisibility(View.GONE);
                                        textView_next.setVisibility(View.VISIBLE);
                                    }else{
                                        textView_next2.setVisibility(View.VISIBLE);
                                        textView_next.setVisibility(View.VISIBLE);
                                    }

                                    textView_next.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            if(position == (total-1)){
                                                textView_next2.setVisibility(View.VISIBLE);
                                                textView_next.setVisibility(View.GONE);
                                            }else{
                                                textView_next2.setVisibility(View.VISIBLE);
                                                textView_next.setVisibility(View.VISIBLE);
                                                list_view.scrollToPosition(position+1);
                                            }

                                        }
                                    });

                                    textView_next2.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            if(position == 0){
                                                textView_next2.setVisibility(View.GONE);
                                                textView_next.setVisibility(View.VISIBLE);
                                            }else{
                                                textView_next2.setVisibility(View.VISIBLE);
                                                textView_next.setVisibility(View.VISIBLE);
                                                list_view.scrollToPosition(position-1);
                                            }
                                        }
                                    });
                                }
                            });



                        } 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;
            }
        }
    }
}
