package com.maxmoney.maxcddconsole.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
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 CddSearchActivity extends AppCompatActivity {
    String session = "";
    StandardProgressDialog standardProgressDialog;
    ImageView imageView_back;
    EditText editText_idNo;
    Button button_search;
    TextView textView_name;
    LinearLayout linear_customer_name;
    String idType = "",idNo = "",email = "",customerName = "";
    TextView textView_continue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cdd_search);

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

        imageView_back = findViewById(R.id.imageView_back);
        editText_idNo = findViewById(R.id.editText_idNo);
        button_search = findViewById(R.id.button_search);
        textView_name = findViewById(R.id.textView_name);
        linear_customer_name = findViewById(R.id.linear_customer_name);
        textView_continue = findViewById(R.id.textView_continue);



        button_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(editText_idNo.getText().toString().equals("")){
                    editText_idNo.setError("Please fill the value");
                }else{
                    SearchUser();
                }
            }
        });

        imageView_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        linear_customer_name.setVisibility(View.GONE);
        textView_continue.setVisibility(View.GONE);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    public void SearchUser(){
        StringRequest stringRequest = new StringRequest(GET, BasedUrl.URL_STAGING +"customers/"+editText_idNo.getText().toString(),
                new com.android.volley.Response.Listener<String>() {
                    @Override
                    public void onResponse(final String response) {
                        standardProgressDialog.dismiss();
                        try {
                            JSONObject obj = new JSONObject(response);
                            if(obj.getString("status").equals("Unapproved")){
                                linear_customer_name.setVisibility(View.VISIBLE);
                                textView_continue.setVisibility(View.VISIBLE);
                                textView_name.setText(obj.getString("customerName"));
                                idType = obj.getString("idType");
                                idNo = obj.getString("idNo");
                                email = obj.getString("email");
                                customerName = obj.getString("customerName");
                                textView_continue.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent next = new Intent(getApplicationContext(),CddActivity.class);
                                        next.putExtra("customerName",customerName);
                                        next.putExtra("email",email);
                                        next.putExtra("idNo",idNo);
                                        next.putExtra("idType",idType);
                                        next.putExtra("session",session);
                                        startActivity(next);
                                    }
                                });
                            }else{
                                linear_customer_name.setVisibility(View.GONE);
                                textView_continue.setVisibility(View.GONE);
                                Toast.makeText(getApplicationContext(),"Customer not found",Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new com.android.volley.Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        standardProgressDialog.dismiss();
                        linear_customer_name.setVisibility(View.GONE);
                        textView_continue.setVisibility(View.GONE);
                        Toast.makeText(getApplicationContext(),"Customer not found",Toast.LENGTH_SHORT).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }

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