package com.maxmoney.maxcddconsole.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
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.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.HashMap;
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;

    TextView ids,name,overTimeInsideBranchHours,overTimeOutsideBranchHours,branchIncentive,allowance,corridorIncentive,
            individualIncentive,claims,deduction,netPay,basicPay,branch;
    @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);

        ids = findViewById(R.id.ids);
        name = findViewById(R.id.name);
        overTimeInsideBranchHours = findViewById(R.id.overTimeInsideBranchHours);
        overTimeOutsideBranchHours = findViewById(R.id.overTimeOutsideBranchHours);
        branchIncentive = findViewById(R.id.branchIncentive);
        allowance = findViewById(R.id.allowance);
        corridorIncentive = findViewById(R.id.corridorIncentive);
        individualIncentive = findViewById(R.id.individualIncentive);
        claims = findViewById(R.id.claims);
        deduction = findViewById(R.id.deduction);
        netPay = findViewById(R.id.netPay);
        basicPay = findViewById(R.id.basicPay);
        branch = findViewById(R.id.branch);


        imageView_back.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        standardProgressDialog.show();
        getDetails();

    }

    private void getDetails() {
        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);

                                ids.setText(objs.getString("id"));
                                name.setText(objs.getString("name"));
                                overTimeInsideBranchHours.setText(objs.getString("overTimeInsideBranchHours"));
                                overTimeOutsideBranchHours.setText(objs.getString("overTimeOutsideBranchHours"));
                                branchIncentive.setText(objs.getString("branchIncentive"));
                                allowance.setText(objs.getString("allowance"));
                                corridorIncentive.setText(objs.getString("corridorIncentive"));
                                individualIncentive.setText(objs.getString("individualIncentive"));
                                claims.setText(objs.getString("claims"));
                                deduction.setText(objs.getString("deduction"));
                                netPay.setText("RM "+objs.getString("netPay"));
                                basicPay.setText("RM "+objs.getString("basicPay"));
                                branch.setText(objs.getString("branch"));

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