package com.tomas.ckd_system;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.tomas.ckd_system.Adapter.EventLog_Adapter;
import com.tomas.ckd_system.Adapter.ScheduleReceiving_Adapter;
import com.tomas.ckd_system.Adapter.Schedule_Adapter;
import com.tomas.ckd_system.List_Class.EventLog_list;
import com.tomas.ckd_system.List_Class.ProductionSchedule_list;
import com.tomas.ckd_system.lib.Settings;
import com.tomas.ckd_system.lib.common.AsyncHttpClient;
import com.tomas.ckd_system.lib.common.CallbackListener;
import com.tomas.ckd_system.lib.common.MessageListener;

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

import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.WebSocket;

public class ProductionScheduleReceive_Activity extends AppCompatActivity implements View.OnClickListener, MessageListener, CallbackListener, ClickRowItemProductionSchedule {
    ScheduleReceiving_Adapter schedule_adapter;
    EventLog_Adapter eventLog_adapter;
    TextView txt_HT;
    EditText edt_pc_no, edt_control, edt_part, edt_packing_date, edt_maker_name, edt_maker_code;
    Spinner spinner_status;
    RecyclerView view_schedule, view_even_log;
    SimpleDateFormat dt_format;
    Calendar calendar;
    String str_dt, str_today, str_packing_date, str_uset_id, str_status;
    int position;
    WsClient wsClient;
    Button btn_search;
    ConstraintLayout layout_dialog, layout_production;

    List<EventLog_list> list_evenlog;
    ArrayList<String> list_status;
    List<ProductionSchedule_list> list_schedule;

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

        // update MessageListener class
        WsClient.getInstance().setOnMessageListener(this);

        layout_production = (ConstraintLayout)findViewById(R.id.layout_production_schedule);
        layout_dialog = (ConstraintLayout)findViewById(R.id.laout_list_dialog);

        str_uset_id = Settings.getLoginUser().getName();

        dt_format = new SimpleDateFormat("yyyy-MM-dd");
        calendar = Calendar.getInstance();
        calendar = (Calendar) calendar.clone();
        str_today = dt_format.format(calendar.getTime());
        Log.d(getString(R.string.tag), "datetime_to_server: " + str_today);

        list_status = new ArrayList<String>();
        list_schedule = new ArrayList<ProductionSchedule_list>();
        list_evenlog = new ArrayList<EventLog_list>();

        txt_HT = (TextView)findViewById(R.id.txt_device_ht);
        txt_HT.setText("Handy terminal #"+Settings.getDeviceHT().getDevice_id());

        btn_search = (Button)findViewById(R.id.btn_search);
        btn_search.setEnabled(false);
        btn_search.setBackgroundResource(R.drawable.button_backgroup_gray);

        edt_pc_no = (EditText) findViewById(R.id.edt_pc_no);
        edt_control = (EditText) findViewById(R.id.edt_control);
        edt_part = (EditText) findViewById(R.id.edt_part);
        edt_maker_name = (EditText)findViewById(R.id.edt_maker_name);
        edt_maker_code = (EditText)findViewById(R.id.edt_maker_code);

        edt_packing_date = (EditText) findViewById(R.id.edt_packing_date);
        edt_packing_date.setText(str_today);
        edt_packing_date.setInputType(InputType.TYPE_NULL);
        edt_packing_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Calendar currentDate = Calendar.getInstance();
                new DatePickerDialog(ProductionScheduleReceive_Activity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        currentDate.set(Calendar.YEAR,year);
                        currentDate.set(Calendar.MONTH,monthOfYear);
                        currentDate.set(Calendar.DATE,dayOfMonth);
                        str_dt = dt_format.format(currentDate.getTime());
                        edt_packing_date.setText(str_dt);
                        Log.d(getString(R.string.tag), "packing_date "+str_dt);
                    }
                }, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
            }
        });

        spinner_status = (Spinner) findViewById(R.id.spinner_status);

        view_schedule = (RecyclerView) findViewById(R.id.view_schedule);
        view_schedule.setHasFixedSize(true);
        view_schedule.setRecycledViewPool(new RecyclerView.RecycledViewPool());
        view_schedule.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        view_schedule.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        view_schedule.setItemAnimator(new DefaultItemAnimator());

        view_even_log = (RecyclerView)findViewById(R.id.view_ng);
        view_even_log.setHasFixedSize(true);
        view_even_log.setRecycledViewPool(new RecyclerView.RecycledViewPool());
        view_even_log.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        view_even_log.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        view_even_log.setItemAnimator(new DefaultItemAnimator());


//        Get_Status();
        Get_Schedule_Top(str_today, "");
    }



    public static void hideKeyboard(@NonNull Activity activity) {
        // Check if no view has focus:
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_search:

                btn_search.setEnabled(false);
                btn_search.setBackgroundResource(R.drawable.button_backgroup_gray);

                hideKeyboard(this);

                str_status = spinner_status.getSelectedItem().toString().trim();
                position = spinner_status.getSelectedItemPosition();
                Log.d(getString(R.string.tag), "spinner SelectedItemPosition: "+String.valueOf(position));

                if (str_status.equals("All")){
                    str_status = "";
                }

                str_packing_date = edt_packing_date.getText().toString().trim();
                if (edt_pc_no.getText().toString().trim().equals("") &&
                        edt_control.getText().toString().trim().equals("") &&
                        edt_part.getText().toString().trim().equals("") &&
                        edt_maker_code.getText().toString().trim().equals("") &&
                        edt_maker_name.getText().toString().trim().equals("")
                ){
                    Get_Schedule_Top(str_packing_date,str_status);
                }else {
                    Get_Schedule(str_packing_date, str_status);
                }

                break;

            case R.id.btn_clear:
                btn_search.setEnabled(true);
                btn_search.setBackgroundResource(R.drawable.button_backgroup_green);

                edt_part.setText("");
                edt_control.setText("");
                edt_pc_no.setText("");
                edt_maker_name.setText("");
                edt_maker_code.setText("");
                edt_packing_date.setText(str_today);
//                spinner_status.setSelection(0);

                list_evenlog.clear();
                eventLog_adapter = new EventLog_Adapter(list_evenlog, ProductionScheduleReceive_Activity.this);
                view_even_log.setAdapter(eventLog_adapter);

                list_schedule.clear();
                schedule_adapter = new ScheduleReceiving_Adapter(list_schedule, ProductionScheduleReceive_Activity.this, ProductionScheduleReceive_Activity.this);
                view_schedule.setAdapter(schedule_adapter);
                break;

            case R.id.btn_main:
                try {
                    JSONObject j_ext;
                    j_ext = new JSONObject();
                    j_ext.put("device_id", Settings.getDeviceNo());
                    j_ext.put("user_id", Settings.getLoginUser().getId());
                    j_ext.put("device_type", getString(R.string.device_type));
                    j_ext.put("pair_id", Settings.getDeviceHT().getDevice_id());
                    j_ext.put("pair_dev_type",getString(R.string.pair_dev_type));
                    WsClient.getInstance().sendMessage(
                            WsProtocol.encapsulation(
                                    WsProtocol.COMMAND.REGISTER_REQUEST.toString(),
                                    j_ext
                            )
                    );

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    if (e.getMessage().trim().equals("The server might crash")){
                        WsClient.getInstance().connect();
                    }
                }
                finish();
                break;

            case R.id.btn_close_view_ng:
                layout_production.setVisibility(View.VISIBLE);
                layout_dialog.setVisibility(View.GONE);
                break;

        }
    }

    private void Get_Schedule_Top(String today, String status) {
        list_schedule.clear();

        try {
            JSONObject json = new JSONObject();
            json.put("cmd", "get_schedule_receiving_top");
            json.put("packing_date", today);
            json.put("status", status);
            new AsyncHttpClient(getApplicationContext()
                    , Settings.getMgmtList().get("web_api") + "/api_tablet.php"
                    , json.toString()
            ) {
                @Override
                public void onFinished(String response, int resp_code) {
                    Log.d(getString(R.string.tag), "response: " + response);

                    try {
                        JSONArray jArray = new JSONArray(response);
                        Log.d(getString(R.string.tag), "response json length: " + jArray.length());

                        if (jArray.length() == 0){
                            list_schedule.clear();
                            Toast.makeText(ProductionScheduleReceive_Activity.this,"Not found schedule",Toast.LENGTH_SHORT).show();

                        }else {
                            for (int i = 0; i < jArray.length(); i++) {
                                list_schedule.add(new ProductionSchedule_list(
                                        jArray.getJSONObject(i).getString("id"),
                                        jArray.getJSONObject(i).getString("pc_no"),
                                        jArray.getJSONObject(i).getString("control_no"),
                                        jArray.getJSONObject(i).getString("dist").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("dist"),
                                        jArray.getJSONObject(i).getString("po_no").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("po_no"),
                                        jArray.getJSONObject(i).getString("maker_code").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("maker_code"),
                                        jArray.getJSONObject(i).getString("maker_name").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("maker_name"),
                                        jArray.getJSONObject(i).getString("dc_part_no").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("dc_part_no"),
                                        jArray.getJSONObject(i).getString("part_name").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("part_name"),
                                        jArray.getJSONObject(i).getString("part_color").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("part_color"),
                                        jArray.getJSONObject(i).getString("ttl_qty").trim().equals("null") ? "0" : jArray.getJSONObject(i).getString("ttl_qty"),
                                        jArray.getJSONObject(i).getString("receive_act_qty").trim().equals("null") ? "0" : jArray.getJSONObject(i).getString("receive_act_qty"),
                                        "",
                                        "",
                                        jArray.getJSONObject(i).getString("receive_pending").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("receive_pending"),
                                        jArray.getJSONObject(i).getString("receive_error").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("receive_error"),
                                        "",
                                        "",
                                        "",
                                        "",
                                        "",
                                        jArray.getJSONObject(i).getString("status"),
                                        jArray.getJSONObject(i).getString("receive_user_id").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("receive_user_id")));

                                Log.d(getString(R.string.tag), "list_schedule for: " + list_schedule.size());
                            }
                            Log.d(getString(R.string.tag), "list_schedule else: " + list_schedule.size());
                        }
                        Log.d(getString(R.string.tag), "list_schedule: " + list_schedule.size());
                        schedule_adapter = new ScheduleReceiving_Adapter(list_schedule, ProductionScheduleReceive_Activity.this, ProductionScheduleReceive_Activity.this);
                        view_schedule.setAdapter(schedule_adapter);

                        Log.d(getString(R.string.tag), "view_schedule: ");
                        btn_search.setEnabled(true);
                        btn_search.setBackgroundResource(R.drawable.button_backgroup_green);
                        Log.d(getString(R.string.tag), "      btn_search.setEnabled(true);: ");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }.execute();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void Get_Schedule(String packing_date, String status) {
        list_schedule.clear();

        try {
            JSONObject json = new JSONObject();
            json.put("cmd", "get_schedule_receiving");
            json.put("packing_date", packing_date);
            json.put("pc_no", edt_pc_no.getText().toString().trim());
            json.put("control_no", edt_control.getText().toString().trim());
            json.put("part_no", edt_part.getText().toString().trim());
            json.put("maker_code", edt_maker_code.getText().toString().trim());
            json.put("maker_name", edt_maker_name.getText().toString().trim());
            json.put("status", status);

            new AsyncHttpClient(getApplicationContext()
                    , Settings.getMgmtList().get("web_api") + "/api_tablet.php"
                    , json.toString()
            ) {
                @Override
                public void onFinished(String response, int resp_code) {
                    Log.d(getString(R.string.tag), "response: " + response);

                    try {
                        JSONArray jArray = new JSONArray(response);
                        Log.d(getString(R.string.tag), "response json length: " + jArray.length());

                        if (jArray.length()==0){
                            list_schedule.clear();
                            Toast.makeText(ProductionScheduleReceive_Activity.this,"Not found schedule",Toast.LENGTH_SHORT).show();

                        }else {
                            for (int i = 0; i < jArray.length(); i++){

                                list_schedule.add(new ProductionSchedule_list(
                                        jArray.getJSONObject(i).getString("id"),
                                        jArray.getJSONObject(i).getString("pc_no"),
                                        jArray.getJSONObject(i).getString("control_no"),
                                        jArray.getJSONObject(i).getString("dist").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("dist"),
                                        jArray.getJSONObject(i).getString("po_no").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("po_no"),
                                        jArray.getJSONObject(i).getString("maker_code").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("maker_code"),
                                        jArray.getJSONObject(i).getString("maker_name").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("maker_name"),
                                        jArray.getJSONObject(i).getString("dc_part_no").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("dc_part_no"),
                                        jArray.getJSONObject(i).getString("part_name").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("part_name"),
                                        jArray.getJSONObject(i).getString("part_color").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("part_color"),
                                        jArray.getJSONObject(i).getString("ttl_qty").trim().equals("null") ? "0" : jArray.getJSONObject(i).getString("ttl_qty"),
                                        jArray.getJSONObject(i).getString("receive_act_qty").trim().equals("null") ? "0" : jArray.getJSONObject(i).getString("receive_act_qty"),
                                        "",
                                       "",
                                        jArray.getJSONObject(i).getString("receive_pending").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("receive_pending"),
                                        jArray.getJSONObject(i).getString("receive_error").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("receive_error"),
                                        "",
                                        "",
                                        "",
                                        "",
                                        "",
                                        jArray.getJSONObject(i).getString("status"),
                                        jArray.getJSONObject(i).getString("receive_user_id").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("receive_user_id")));
                            }
                        }
                        schedule_adapter = new ScheduleReceiving_Adapter(list_schedule, ProductionScheduleReceive_Activity.this, ProductionScheduleReceive_Activity.this);
                        view_schedule.setAdapter(schedule_adapter);

                        btn_search.setEnabled(true);
                        btn_search.setBackgroundResource(R.drawable.button_backgroup_green);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }.execute();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMessage(WebSocket webSocket, String text) {
        Log.d(getString(R.string.tag), String.format("%s : received -> \"%s\"", this.getLocalClassName(),
                text));
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                try {
                    JSONObject jsonObject = new JSONObject(text);
                    JSONObject j_ext;

                    switch (WsProtocol.COMMAND.valueOf(jsonObject.getString("cmd"))) {
                        case DATA_REDRAW_REQUEST:
                            spinner_status.setSelection(position);
                            str_status = spinner_status.getSelectedItem().toString().trim();
                            if (str_status.equals("All")) {
                                str_status = "";
                            }

                            str_packing_date = edt_packing_date.getText().toString().trim();
                            if (edt_pc_no.getText().toString().trim().equals("") &&
                                    edt_control.getText().toString().trim().equals("") &&
                                    edt_part.getText().toString().trim().equals("") &&
                                    edt_maker_code.getText().toString().trim().equals("") &&
                                    edt_maker_name.getText().toString().trim().equals("")
                            ){
                                Get_Schedule_Top(str_packing_date,str_status);
                            }else {
                                Get_Schedule(str_packing_date, str_status);
                            }

                            break;

                        case HELLO:
                            j_ext = new JSONObject(jsonObject.getString("ext"));
                            WsClient.getInstance().client_id = j_ext.getInt("id");

                            j_ext = new JSONObject();
                            j_ext.put("device_id", Settings.getDeviceNo());
                            j_ext.put("user_id", Settings.getLoginUser().getId());
                            j_ext.put("device_type", getString(R.string.device_type));
                            j_ext.put("pair_id", Settings.getDeviceHT().getDevice_id());
                            j_ext.put("pair_dev_type", getString(R.string.pair_dev_type));
                            WsClient.getInstance().sendMessage(
                                    WsProtocol.encapsulation(
                                            WsProtocol.COMMAND.REGISTER_REQUEST.toString(),
                                            j_ext
                                    )
                            );
                            break;

                        case AREA_CHANGE_NOTIFY_REQUEST:
                            Log.d(getString(R.string.tag), "cmd : AREA_CHANGE_NOTIFY_REQUEST");

                            j_ext = new JSONObject(jsonObject.getString("ext"));
                            String user_id = j_ext.getString("user_id");
                            String device_id = j_ext.getString("device_id");
                            String device_type = j_ext.getString("device_type");
                            String str_stage = j_ext.getString("area");
                            Log.d(getString(R.string.tag), "AREA_CHANGE :" + user_id + "|" + device_id + "|" + device_type + "|" + str_stage);

                            Settings.setArea_stage(str_stage);

                            WsClient.getInstance().sendMessage(
                                    WsProtocol.encapsulation(
                                            WsProtocol.COMMAND.AREA_CHANGE_NOTIFY_ACK.toString(),
                                            j_ext
                                    )
                            );
//                            finish();
                            break;

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    if (e.getMessage().trim().equals("The server might crash")){
                        WsClient.getInstance().connect();
                    }
                }

            }
        });
    }

    @Override
    public void onFinished(String result) {

    }

    @Override
    public void SelectionItemRow(String receive_pending, String receive_error,
                                 String staging_pending, String staging_error,
                                 String outer_pending, String outer_error, String outer_stopcase) {

        Log.d(getString(R.string.tag), "SelectionItemRow :" + receive_pending + "|" + receive_error + "|"
                + staging_pending + "|" + staging_error+ "|" + outer_pending + "|" + outer_error+ "|" + outer_stopcase);

        if (receive_error.trim().equals("") && receive_pending.trim().equals("")
                && staging_error.trim().equals("") && staging_pending.trim().equals("")
                && outer_error.trim().equals("") && outer_pending.trim().equals("") && outer_stopcase.trim().equals("")){

            new SweetAlertDialog(ProductionScheduleReceive_Activity.this, SweetAlertDialog.NORMAL_TYPE)
                    .setContentText("No items NG & Pending")
                    .show();

        }else {
            layout_production.setVisibility(View.GONE);
            layout_dialog.setVisibility(View.VISIBLE);

            list_evenlog.clear();
            try {
                JSONObject json = new JSONObject();
                json.put("cmd", "get_even_log");
                json.put("receive_pending", receive_pending);
                json.put("receive_error", receive_error);
                json.put("staging_pending", staging_pending);
                json.put("staging_error", staging_error);
                json.put("outer_pending", outer_pending);
                json.put("outer_error", outer_error);
                json.put("outer_stopcase", outer_stopcase);

                new AsyncHttpClient(getApplicationContext()
                        , Settings.getMgmtList().get("web_api") + "/api_tablet.php"
                        , json.toString()
                ) {
                    @Override
                    protected void onFinished(String response, int resp_code) {
                        Log.d(getString(R.string.tag), "response: " + response);

                        try {
                            JSONArray jArray = new JSONArray(response);
                            Log.d(getString(R.string.tag), "response json length: " + jArray.length());

                            for (int i = 0; i < jArray.length(); i++) {
                                JSONObject obj = jArray.getJSONObject(i);

                                String create_dt = jArray.getJSONObject(i).getString("create_dt");
                                if (create_dt.trim().equals("")||create_dt.trim().equals("null")){
                                    create_dt = "";
                                }else {
                                    JSONObject array_date = obj.getJSONObject("create_dt");
                                    create_dt = array_date.getString("date");
                                    create_dt = create_dt.substring(0,19);
                                }

                                list_evenlog.add(new EventLog_list(
                                        jArray.getJSONObject(i).getString("id"),
                                        jArray.getJSONObject(i).getString("job_id"),
                                        jArray.getJSONObject(i).getString("schedule_id"),
                                        jArray.getJSONObject(i).getString("event"),
                                        jArray.getJSONObject(i).getString("event_det").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("event_det"),
                                        jArray.getJSONObject(i).getString("error_code"),
                                        jArray.getJSONObject(i).getString("error_det").trim().equals("null") ? "" : jArray.getJSONObject(i).getString("error_det"),
                                        jArray.getJSONObject(i).getString("log_level"),
                                        jArray.getJSONObject(i).getString("status"),
                                        jArray.getJSONObject(i).getString("stage"),
                                        jArray.getJSONObject(i).getString("device_type"),
                                        jArray.getJSONObject(i).getString("device_id"),
                                        create_dt,
                                        jArray.getJSONObject(i).getString("create_user_id")));
                            }

                            eventLog_adapter = new EventLog_Adapter(list_evenlog, ProductionScheduleReceive_Activity.this);
                            view_even_log.setAdapter(eventLog_adapter);


                        } catch (JSONException e) {
                            new SweetAlertDialog(ProductionScheduleReceive_Activity.this, SweetAlertDialog.ERROR_TYPE)
                                    .setTitleText("ERROR")
                                    .setContentText(e.getMessage())
                                    .show();
                        }
                    }

                }.execute();
            }catch (JSONException e) {
                new SweetAlertDialog(ProductionScheduleReceive_Activity.this, SweetAlertDialog.ERROR_TYPE)
                        .setTitleText("ERROR")
                        .setContentText(e.getMessage())
                        .show();
            }
        }
    }
}