package com.cygnusa.hyperm.dbadapters;

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

import com.cygnusa.hyperm.util.CommonMethods;
import com.cygnusa.hyperm.util.GlobalData;
import com.cygnusa.hyperm.util.StorePreference;
import com.cygnusa.hyperm.wrappers.SendAllCustomerData;

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

public class CustomerDetailsAdapter implements TableConstants {

    private static Context context;
    private StorePreference storePreference;

    public CustomerDetailsAdapter(Context context) {
        this.context = context;
        storePreference = new StorePreference(context);
    }

    public static boolean checkRecordExists(String toString) {

        CommonDBHelper.getInstance(context).open();
        String selectQuery = " SELECT * FROM " + TABLE_CUSTOMER_DETAILS + " WHERE " + CUST_MOBILE + " = '" + toString + "'";
        Cursor cursor = CommonDBHelper.getInstance(context).getDb().rawQuery(selectQuery, null);
        cursor.moveToFirst();
        try {
            if (cursor.getCount() <= 0) {
                return false;
            }
        } finally {
            cursor.close();
        }

        return true;

    }

    public void insertLoginDetails(String currentTime, String selectedItem, String name, String mobile, String email, String ageRange) {

        CommonDBHelper.getInstance(context).open();
        ContentValues values = new ContentValues();
        values.put(CUST_CURRENT_TIME, currentTime);
        values.put(CUST_SAUTATION, selectedItem);
        values.put(CUST_NAME, name);
        values.put(CUST_MOBILE, mobile);
        values.put(CUST_EMAIL, email);
        values.put(CUST_AGERANGE, ageRange);
        values.put(CUST_EMP_CODE, GlobalData.user.getData().getEmp_code());
        values.put(CUST_SMPIN, GlobalData.user.getData().getSmpin());

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

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

    }

    public List<SendAllCustomerData> getAllDataFromDB() {

        CommonDBHelper.getInstance(context).open();

        List<SendAllCustomerData> allCustomerData;
        SendAllCustomerData customerData;
        allCustomerData = new ArrayList<>();

        String query = "SELECT * FROM " + TABLE_CUSTOMER_DETAILS;

        Cursor cursor = CommonDBHelper.getInstance(context).getDb().rawQuery(query, null);
        try {
            if (cursor.moveToFirst()) {
                do {
                    customerData = new SendAllCustomerData();
                    customerData.setSalutation(cursor.getString(2));
                    customerData.setFirstname(cursor.getString(3));
                    customerData.setMobile(cursor.getString(4));
                    customerData.setEmail(cursor.getString(5));
                    customerData.setAge(cursor.getString(6));
                    customerData.setUid(cursor.getString(1));
                    allCustomerData.add(customerData);
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cursor.close();
        }

        return allCustomerData;
    }
}
