package com.renitha.Activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.renitha.Interface.APIService;
import com.renitha.Model.APIUrl;
import com.renitha.Model.SetterCategory;
import com.renitha.Model.SetterComplaintType;
import com.renitha.Model.SetterLogin;
import com.renitha.Model.SetterMake;
import com.renitha.Model.SetterModel;
import com.renitha.Model.SetterProduct;
import com.renitha.Model.SetterProfile;
import com.renitha.Model.SetterSerialNo;
import com.renitha.Model.SetterTicket;
import com.renitha.Model.SetterTicketPriority;
import com.renitha.Pref.Pref;
import com.renitha.R;
import com.renitha.Util.Util;

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

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AddComplaintActivity extends AppCompatActivity
{
    private EditText editTextDate,editTextEnquiryDetails,editTextRemark;
    private Spinner spinnerComplaintType,spinnerCategoryName,spinnerProductName,spinnerMakeName,spinnerModelName,spinnerTicketPriority,spinnerSerialNo;
    private Button btnSubmit;

    private static SimpleDateFormat dateFormat;
    private int year;
    private int month;
    private int day;
    private String complaintType,ticketPriority;
    SetterComplaintType myItem;
    ArrayList<SetterComplaintType> compTypeList;

    SetterTicketPriority myItem1;
    ArrayList<SetterTicketPriority> ticketPriorityList;

    private SetterCategory mSetterCategoryName;
    private ArrayList<SetterCategory> mListCategoryName;
    private String mSpinnerCategoryId,selectedCategoryValue,selectedCategoryId="-1";

    private SetterProduct mSetterProductName;
    private ArrayList<SetterProduct> mListProductName;
    private String mSpinnerProductId,selectedProductValue,selectedProductId="-1";

    private SetterMake mSetterMakeName;
    private ArrayList<SetterMake> mListMakeName;
    private String mSpinnerMakeId,selectedMakeValue,selectedMakeId="-1";

    private SetterModel mSetterModelName;
    private ArrayList<SetterModel> mListModelName;
    private String mSpinnerModelId,selectedModelValue,selectedModelId="-1";

    private SetterSerialNo mSetterSerialName;
    private ArrayList<SetterSerialNo> mListSerialName;
    private String mSpinnerSerialId,selectedSerialValue,selectedSerialId="-1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_complaint);
        editTextDate                =   (EditText)findViewById(R.id.editTextDate);
        editTextEnquiryDetails      =   (EditText)findViewById(R.id.editTextEnquiryDetails);
        editTextRemark              =   (EditText)findViewById(R.id.editTextRemark);
        spinnerComplaintType        =   (Spinner)findViewById(R.id.spinnerComplaintType);
        spinnerCategoryName         =   (Spinner)findViewById(R.id.spinnerCategoryName);
        spinnerProductName          =   (Spinner)findViewById(R.id.spinnerProductName);
        spinnerMakeName             =   (Spinner)findViewById(R.id.spinnerMakeName);
        spinnerModelName            =   (Spinner)findViewById(R.id.spinnerModelName);
        spinnerSerialNo             =   (Spinner)findViewById(R.id.spinnerSerialNo);
     //   spinnerSerialNo             =   (EditText)findViewById(R.id.spinnerSerialNo);
        spinnerTicketPriority       =   (Spinner)findViewById(R.id.spinnerTicketPriority);

        btnSubmit                   =   (Button)findViewById(R.id.btnSubmit);
        editTextDate.setEnabled(false);
        dateFormat                  =    new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
        final Calendar c            =    Calendar.getInstance();
        year                        =    c.get(Calendar.YEAR);
        month                       =    c.get(Calendar.MONTH);
        day                         =    c.get(Calendar.DAY_OF_MONTH);
        editTextDate.setText(checkDigit(day)+"-"+checkDigit(month+1)+"-"+year);

        String category_url         =       APIUrl.BASE_URL+"api_category_list.php?select=select&login_id="+ Pref.getInstance(getApplicationContext()).getLoginId();
        setData();
        setCategory(category_url);
        spinnerComplaintType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                SetterComplaintType bldgp = (SetterComplaintType) parent.getSelectedItem();
                complaintType = bldgp.getId();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });

        spinnerTicketPriority.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                SetterTicketPriority mari = (SetterTicketPriority) parent.getSelectedItem();
                ticketPriority = mari.getId();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });

        spinnerCategoryName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                mSpinnerCategoryId = mListCategoryName.get(position).getId();
                if (mListCategoryName.get(position).getId().equalsIgnoreCase("-1"))
                {
                    Toast.makeText(getApplicationContext(), "Please select Categoty ", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Log.d("test", "Selected Payer Id ==========> " + mListCategoryName.get(position).getId());

                    mSetterCategoryName     =   mListCategoryName.get(position);
                    selectedCategoryId      =   mListCategoryName.get(position).getId();
                    selectedCategoryValue   =   mListCategoryName.get(position).getName();
                    String product_url      =   APIUrl.BASE_URL+"api_product_list.php?select=select&login_id="+ Pref.getInstance(getApplicationContext()).getLoginId()+"&category_name="+selectedCategoryValue;
                    String p                =   product_url.replaceAll(" ", "%20");
                    setProduct(p);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });

        spinnerProductName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                mSpinnerProductId = mListProductName.get(position).getId();
                if (mListProductName.get(position).getId().equalsIgnoreCase("-1"))
                {
                    Toast.makeText(getApplicationContext(), "Please select Product ", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Log.d("test", "Selected Payer Id ==========> " + mListProductName.get(position).getId());

                    mSetterProductName      =   mListProductName.get(position);
                    selectedProductId       =   mListProductName.get(position).getId();
                    selectedProductValue    =   mListProductName.get(position).getName();
                    String make_url         =   APIUrl.BASE_URL+"api_make_list.php?select=select&login_id="+ Pref.getInstance(getApplicationContext()).getLoginId()+"&product_name="+selectedProductValue+"&category_name="+selectedCategoryValue;
                    String make_url1        =   make_url.replaceAll(" ", "%20");
                    setMake(make_url1);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });

        spinnerMakeName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                mSpinnerMakeId = mListMakeName.get(position).getId();
                if (mListMakeName.get(position).getId().equalsIgnoreCase("-1"))
                {
                    Toast.makeText(getApplicationContext(), "Please select Make ", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Log.d("test", "Selected Payer Id ==========> " + mListMakeName.get(position).getId());

                    mSetterMakeName     =   mListMakeName.get(position);
                    selectedMakeId      =   mListMakeName.get(position).getId();
                    selectedMakeValue   =   mListMakeName.get(position).getName();
                    String model_url    =   APIUrl.BASE_URL+"api_model_list.php?select=select&login_id="+ Pref.getInstance(getApplicationContext()).getLoginId()+"&make_name="+selectedMakeValue+"&product_name="+selectedProductValue+"&category_name="+selectedCategoryValue;
                    String model_url1   =   model_url.replaceAll(" ", "%20");
                    setModel(model_url1);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });
        spinnerModelName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                mSpinnerModelId = mListModelName.get(position).getId();
                if (mListModelName.get(position).getId().equalsIgnoreCase("-1"))
                {
                    Toast.makeText(getApplicationContext(), "Please select Model ", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Log.d("test", "Selected Payer Id ==========> " + mListModelName.get(position).getId());

                    mSetterModelName     =   mListModelName.get(position);
                    selectedModelId      =   mListModelName.get(position).getId();
                    selectedModelValue   =   mListModelName.get(position).getName();
                    String serial_url    =   APIUrl.BASE_URL+"api_serial_list.php?select=select&login_id="+ Pref.getInstance(getApplicationContext()).getLoginId()+"&model_name="+selectedModelValue+"&make_name="+selectedMakeValue+"&product_name="+selectedProductValue+"&category_name="+selectedCategoryValue;
                    setSerial(serial_url);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });
        spinnerSerialNo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                mSpinnerSerialId = mListSerialName.get(position).getId();
                if (mListSerialName.get(position).getId().equalsIgnoreCase("-1"))
                {
                    Toast.makeText(getApplicationContext(), "Please select Serial No ", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Log.d("test", "Selected Payer Id ==========> " + mListSerialName.get(position).getId());

                    mSetterSerialName     =   mListSerialName.get(position);
                    selectedSerialId      =   mListSerialName.get(position).getId();
                    selectedSerialValue   =   mListSerialName.get(position).getName();
                    get_pms_call();
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(selectedCategoryId.equalsIgnoreCase("-1") )
                {
                    showAlert("Please select category");
                }
                else if(selectedProductId.equalsIgnoreCase("-1") )
                {
                    showAlert("Please select Product");
                }
                else if(selectedMakeId.equalsIgnoreCase("-1") )
                {
                    showAlert("Please select Make");
                }
                else if(selectedModelId.equalsIgnoreCase("-1") )
                {
                    showAlert("Please select Model");
                }else if(selectedSerialId.equalsIgnoreCase("-1") )
                {
                    showAlert("Please select Serial No.");
                }
                else if(editTextEnquiryDetails.getText().toString().equals(""))
                {
                    showAlert("Please enter enquiry details");
                }
                else
                {
                    save_complaint();
                }
            }
        });
    }
    private void setData() {

        compTypeList = new ArrayList<>();
        compTypeList.add(new SetterComplaintType("Break Down", "Break Down"));
        //fill data in spinner
        ArrayAdapter<SetterComplaintType> adapter = new ArrayAdapter<SetterComplaintType>(getApplicationContext(), R.layout.text_method_spin, compTypeList);
        spinnerComplaintType.setAdapter(adapter);
        spinnerComplaintType.setSelection(adapter.getPosition(myItem));//Optional to set the selected item.

        ticketPriorityList = new ArrayList<>();
        ticketPriorityList.add(new SetterTicketPriority("High", "High"));
        ticketPriorityList.add(new SetterTicketPriority("Medium", "Medium"));
        ticketPriorityList.add(new SetterTicketPriority("Low", "Low"));
        //fill data in spinner
        ArrayAdapter<SetterTicketPriority> adapter1 = new ArrayAdapter<SetterTicketPriority>(getApplicationContext(), R.layout.text_method_spin, ticketPriorityList);
        spinnerTicketPriority.setAdapter(adapter1);
        spinnerTicketPriority.setSelection(adapter1.getPosition(myItem1));//Optional to set the selected item.
    }

    private void setCategory(String url)
    {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new com.android.volley.Response.Listener<String>() {
            @Override
            public void onResponse(String response)
            {
                try
                {
                    JSONObject jsonObject = new JSONObject(response);
                    mListCategoryName= new ArrayList<SetterCategory>();
                    SetterCategory productss = new SetterCategory();
                    productss.setName("Select Category");
                    productss.setId("-1");
                    mListCategoryName.add(productss);
                    if (jsonObject.getInt("status") == 200)
                    {
                        JSONArray array = jsonObject.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++)
                        {
                            JSONObject jsoObject = array.getJSONObject(i);
                            SetterCategory prod = new SetterCategory();
                            prod.setId(jsoObject.getString("category_id"));
                            prod.setName(jsoObject.getString("category_name"));
                            mListCategoryName.add(prod);
                        }
                        ArrayAdapter<SetterCategory> mAdapter;
                        mAdapter = new ArrayAdapter<SetterCategory>(getApplicationContext(), R.layout.text_method_spin, mListCategoryName)
                        {
                            @Override
                            public View getView(int position, View convertView, ViewGroup parent)
                            {
                                View v = super.getView(position, convertView, parent);
                                TextView textView = (TextView) v.findViewById(R.id.txtMethodSpinner);
                                textView.setText(mListCategoryName.get(position).getName() + " ");
                                return v;
                            }
                            @Override
                            public View getDropDownView(int position, View convertView, ViewGroup parent)
                            {
                                View v = super.getView(position, convertView, parent);
                                TextView textView = (TextView) v.findViewById(R.id.txtMethodSpinner);
                                textView.setText(mListCategoryName.get(position).getName() + " ");
                                return v;
                            }
                        };
                        spinnerCategoryName.setAdapter(mAdapter);
                    }
                    else
                    {
                        Util.showDialogMessage(getApplicationContext(), jsonObject.getString("message"));
                    }
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        }, new com.android.volley.Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                error.printStackTrace();
            }
        });
        int socketTimeout = 300000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }
private void setProduct(String url)
    {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new com.android.volley.Response.Listener<String>() {
            @Override
            public void onResponse(String response)
            {
                try
                {
                    JSONObject jsonObject = new JSONObject(response);
                    mListProductName= new ArrayList<SetterProduct>();
                    SetterProduct productss = new SetterProduct();
                    productss.setName("Select Product");
                    productss.setId("-1");
                    mListProductName.add(productss);
                    if (jsonObject.getInt("status") == 200)
                    {
                        JSONArray array = jsonObject.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++)
                        {
                            JSONObject jsoObject = array.getJSONObject(i);
                            SetterProduct prod = new SetterProduct();
                            prod.setId(jsoObject.getString("product_id"));
                            prod.setName(jsoObject.getString("product_name"));
                            mListProductName.add(prod);
                        }
                        ArrayAdapter<SetterProduct> mAdapter;
                        mAdapter = new ArrayAdapter<SetterProduct>(getApplicationContext(), R.layout.text_method_spin, mListProductName)
                        {
                            @Override
                            public View getView(int position, View convertView, ViewGroup parent)
                            {
                                View v = super.getView(position, convertView, parent);
                                TextView textView = (TextView) v.findViewById(R.id.txtMethodSpinner);
                                textView.setText(mListProductName.get(position).getName() + " ");
                                return v;
                            }
                            @Override
                            public View getDropDownView(int position, View convertView, ViewGroup parent)
                            {
                                View v = super.getView(position, convertView, parent);
                                TextView textView = (TextView) v.findViewById(R.id.txtMethodSpinner);
                                textView.setText(mListProductName.get(position).getName() + " ");
                                return v;
                            }
                        };
                        spinnerProductName.setAdapter(mAdapter);
                    }
                    else
                    {
                        Util.showDialogMessage(getApplicationContext(), jsonObject.getString("message"));
                    }
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        }, new com.android.volley.Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                error.printStackTrace();
            }
        });
        int socketTimeout = 300000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }

    private void setMake(String url)
    {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new com.android.volley.Response.Listener<String>() {
            @Override
            public void onResponse(String response)
            {
                try
                {
                    JSONObject jsonObject = new JSONObject(response);
                    mListMakeName= new ArrayList<SetterMake>();
                    SetterMake productss = new SetterMake();
                    productss.setName("Select Make");
                    productss.setId("-1");
                    mListMakeName.add(productss);
                    if (jsonObject.getInt("status") == 200)
                    {
                        JSONArray array = jsonObject.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++)
                        {
                            JSONObject jsoObject = array.getJSONObject(i);
                            SetterMake prod = new SetterMake();
                            prod.setId(jsoObject.getString("make_id"));
                            prod.setName(jsoObject.getString("make_name"));
                            mListMakeName.add(prod);
                        }
                        ArrayAdapter<SetterMake> mAdapter;
                        mAdapter = new ArrayAdapter<SetterMake>(getApplicationContext(), R.layout.text_method_spin, mListMakeName)
                        {
                            @Override
                            public View getView(int position, View convertView, ViewGroup parent)
                            {
                                View v = super.getView(position, convertView, parent);
                                TextView textView = (TextView) v.findViewById(R.id.txtMethodSpinner);
                                textView.setText(mListMakeName.get(position).getName() + " ");
                                return v;
                            }
                            @Override
                            public View getDropDownView(int position, View convertView, ViewGroup parent)
                            {
                                View v = super.getView(position, convertView, parent);
                                TextView textView = (TextView) v.findViewById(R.id.txtMethodSpinner);
                                textView.setText(mListMakeName.get(position).getName() + " ");
                                return v;
                            }
                        };
                        spinnerMakeName.setAdapter(mAdapter);
                    }
                    else
                    {
                        Util.showDialogMessage(getApplicationContext(), jsonObject.getString("message"));
                    }
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        }, new com.android.volley.Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                error.printStackTrace();
            }
        });
        int socketTimeout = 300000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }
    private void setModel(String url)
    {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new com.android.volley.Response.Listener<String>() {
            @Override
            public void onResponse(String response)
            {
                try
                {
                    JSONObject jsonObject = new JSONObject(response);
                    mListModelName= new ArrayList<SetterModel>();
                    SetterModel productss = new SetterModel();
                    productss.setName("Select Model");
                    productss.setId("-1");
                    mListModelName.add(productss);
                    if (jsonObject.getInt("status") == 200)
                    {
                        JSONArray array = jsonObject.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++)
                        {
                            JSONObject jsoObject = array.getJSONObject(i);
                            SetterModel prod = new SetterModel();
                            prod.setId(jsoObject.getString("model_id"));
                            prod.setName(jsoObject.getString("model_name"));
                            mListModelName.add(prod);
                        }
                        ArrayAdapter<SetterModel> mAdapter;
                        mAdapter = new ArrayAdapter<SetterModel>(getApplicationContext(), R.layout.text_method_spin, mListModelName)
                        {
                            @Override
                            public View getView(int position, View convertView, ViewGroup parent)
                            {
                                View v = super.getView(position, convertView, parent);
                                TextView textView = (TextView) v.findViewById(R.id.txtMethodSpinner);
                                textView.setText(mListModelName.get(position).getName() + " ");
                                return v;
                            }
                            @Override
                            public View getDropDownView(int position, View convertView, ViewGroup parent)
                            {
                                View v = super.getView(position, convertView, parent);
                                TextView textView = (TextView) v.findViewById(R.id.txtMethodSpinner);
                                textView.setText(mListModelName.get(position).getName() + " ");
                                return v;
                            }
                        };
                        spinnerModelName.setAdapter(mAdapter);
                    }
                    else
                    {
                        Util.showDialogMessage(getApplicationContext(), jsonObject.getString("message"));
                    }
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        }, new com.android.volley.Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                error.printStackTrace();
            }
        });
        int socketTimeout = 300000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }
    private void setSerial(String url)
    {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new com.android.volley.Response.Listener<String>() {
            @Override
            public void onResponse(String response)
            {
                try
                {
                    JSONObject jsonObject = new JSONObject(response);
                    mListSerialName= new ArrayList<SetterSerialNo>();
                    SetterSerialNo productss = new SetterSerialNo();
                    productss.setName("Select Serial No");
                    productss.setId("-1");
                    mListSerialName.add(productss);
                    if (jsonObject.getInt("status") == 200)
                    {
                        JSONArray array = jsonObject.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++)
                        {
                            JSONObject jsoObject = array.getJSONObject(i);
                            SetterSerialNo prod = new SetterSerialNo();
                            prod.setId(jsoObject.getString("serial_id"));
                            prod.setName(jsoObject.getString("serial_name"));
                            mListSerialName.add(prod);
                        }
                        ArrayAdapter<SetterSerialNo> mAdapter;
                        mAdapter = new ArrayAdapter<SetterSerialNo>(getApplicationContext(), R.layout.text_method_spin, mListSerialName)
                        {
                            @Override
                            public View getView(int position, View convertView, ViewGroup parent)
                            {
                                View v = super.getView(position, convertView, parent);
                                TextView textView = (TextView) v.findViewById(R.id.txtMethodSpinner);
                                textView.setText(mListSerialName.get(position).getName() + " ");
                                return v;
                            }
                            @Override
                            public View getDropDownView(int position, View convertView, ViewGroup parent)
                            {
                                View v = super.getView(position, convertView, parent);
                                TextView textView = (TextView) v.findViewById(R.id.txtMethodSpinner);
                                textView.setText(mListSerialName.get(position).getName() + " ");
                                return v;
                            }
                        };
                        spinnerSerialNo.setAdapter(mAdapter);
                    }
                    else
                    {
                        Util.showDialogMessage(getApplicationContext(), jsonObject.getString("message"));
                    }
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        }, new com.android.volley.Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                error.printStackTrace();
            }
        });
        int socketTimeout = 300000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }
    private void get_pms_call()
    {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Wait...");
        progressDialog.show();
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(APIUrl.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        APIService service = retrofit.create(APIService.class);

        String serial_no = selectedSerialValue;
        Call<SetterTicket> call = service.getPMSCallCount(Pref.getInstance(getApplicationContext()).getLoginId(),Pref.getInstance(getApplicationContext()).getLoginType(),complaintType,selectedCategoryValue,selectedProductValue,selectedMakeValue,selectedModelValue,serial_no);
        call.enqueue(new Callback<SetterTicket>()
        {
            @Override
            public void onResponse(Call<SetterTicket> call, Response<SetterTicket> response)
            {
                progressDialog.dismiss();
                if (response.body().getError().equals(true))
                {
                    if(response.body().getTicket_id().equals("1"))
                    {
                        btnSubmit.setEnabled(false);
                        Toast.makeText(getApplicationContext(), "You have reached maximum no. of tickets for this product. Please contact to our support team.", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        btnSubmit.setEnabled(true);
                    }
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFailure(Call<SetterTicket> call, Throwable t)
            {
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
            }
        });
    }

    private void save_complaint()
    {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Wait...");
        progressDialog.show();
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(APIUrl.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        APIService service = retrofit.create(APIService.class);

        String serial_no = selectedSerialValue;
        String equip_cond = editTextEnquiryDetails.getText().toString();
        String remark     = editTextRemark.getText().toString();
        Call<SetterTicket> call = service.saveComplaint(Pref.getInstance(getApplicationContext()).getLoginId(),Pref.getInstance(getApplicationContext()).getLoginType(),complaintType,selectedCategoryValue,selectedProductValue,selectedMakeValue,selectedModelValue,serial_no,ticketPriority,equip_cond,remark);
        call.enqueue(new Callback<SetterTicket>()
        {
            @Override
            public void onResponse(Call<SetterTicket> call, Response<SetterTicket> response)
            {
                progressDialog.dismiss();
                if (response.body().getError().equals(true))
                {
                    Pref.getInstance(getApplicationContext()).setTICKET_ID(response.body().getTicket_id());
                    //Toast.makeText(getApplicationContext(), "Complaint submitted successfully", Toast.LENGTH_LONG).show();
                    startActivity(new Intent(getApplicationContext(), UploadPhotoCustomerActivity.class));
                    overridePendingTransition(R.anim.enter, R.anim.exit);
                    //startActivity(new Intent(getApplicationContext(), ViewCustomerComplaints.class));
                    //overridePendingTransition(R.anim.enter, R.anim.exit);
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFailure(Call<SetterTicket> call, Throwable t)
            {
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
            }
        });
    }

    public static String checkDigit(int number)
    {
        return number <= 9 ? "0" + number : String.valueOf(number);
    }
    public void showAlert(String message)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(Html.fromHtml("<font color='#8472BB'>"+message+"!</font>")).setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {
                        dialog.dismiss();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
        Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
        pbutton.setTextColor(Color.BLACK);
    }
}
