package com.cygnusa.hyperm.dbadapters;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.cygnusa.hyperm.util.CommonMethods;
import com.cygnusa.hyperm.util.GlobalData;
import com.cygnusa.hyperm.wrappers.SendEnquiryDataModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CustomerEnquiryDataAdapters implements TableConstants {
    private Context context;

    public CustomerEnquiryDataAdapters(Context context) {
        this.context = context;
    }

    public boolean checkRecordExists(String toString) {

        CommonDBHelper.getInstance(context).open();
        String selectQuery = " SELECT * FROM " + TABLE_ENQUIRY_DETAILS + " WHERE " + ENQUIRY_MOBILE + " = '" + toString + "'";
        Cursor cursor = null;
        try {
        cursor = CommonDBHelper.getInstance(context).getDb().rawQuery(selectQuery, null);
        cursor.moveToFirst();

            if (cursor.getCount() <= 0) {
                return false;
            }
        }catch(Exception e){
            Log.e("jjj",e.toString());
        }
        finally {
            cursor.close();
        }

        return true;
    }

    public void insertEnquiryDetails(HashMap<String, String> enquiryDetailsMap) {

        CommonDBHelper.getInstance(context).open();
        ContentValues values = new ContentValues();
        values.put(ENQUIRY_CURRENT_TIME, enquiryDetailsMap.get("current_time"));
        values.put(ENQUITY_VARIANT_ID, enquiryDetailsMap.get("variant_id"));
        values.put(ENQUIRY_NAME, enquiryDetailsMap.get("name"));
        values.put(ENQUIRY_MODELID, enquiryDetailsMap.get("model_id"));
        values.put(ENQUIRY_MOBILE, enquiryDetailsMap.get("mobile"));
        values.put(ENQUIRY_ISBROUCHER, enquiryDetailsMap.get("is_brochure"));
        values.put(ENQUIRY_EMAIL, enquiryDetailsMap.get("email"));
        values.put(ENQUIRY_EMP_CODE, GlobalData.user.getData().getEmp_code());
        values.put(ENQUIRY_SMPIN, GlobalData.user.getData().getSmpin());

        CommonMethods.showToast(context, "Value Stored").show();

        CommonDBHelper.getInstance(context).getDb().insert(TABLE_ENQUIRY_DETAILS, null, values);

    }

    public List<SendEnquiryDataModel> getAllEnquiryFromTable() {

        CommonDBHelper.getInstance(context).open();

        List<SendEnquiryDataModel> allEnquiryData;
        SendEnquiryDataModel enquiryData;
        allEnquiryData = new ArrayList<>();

        String query = "SELECT * FROM " + TABLE_ENQUIRY_DETAILS;

        try (Cursor cursor = CommonDBHelper.getInstance(context).getDb().rawQuery(query, null)) {
            if (cursor.moveToFirst()) {
                do {
                    enquiryData = new SendEnquiryDataModel();
                    enquiryData.setUid(cursor.getString(1));
                    enquiryData.setVariantId(cursor.getString(2));
                    enquiryData.setName(cursor.getString(3));
                    enquiryData.setMobile(cursor.getString(4));
                    enquiryData.setEmail(cursor.getString(5));
                    enquiryData.setIsBrochure(cursor.getString(6));
                    enquiryData.setModelId(Integer.valueOf(cursor.getString(7)));
                    allEnquiryData.add(enquiryData);
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allEnquiryData;

    }
}
