package com.xplora.xpsettings.Data;

import static com.xplora.xpsettings.Utils.MetaData.DB_GOPLAY_APN;
import static com.xplora.xpsettings.Utils.MetaData.DB_GOPLAY_RINGTONES;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.xplora.xpsettings.Model.ApnModel;
import com.xplora.xpsettings.Model.RingtoneModel;
import com.xplora.xpsettings.Utils.Macros;
import com.xplora.xpsettings.Utils.MetaData;

import java.util.ArrayList;

public class DatabaseHelper {
    public static String TAG = "losion / xpsettings :" + "DatabaseHelper";

    @SuppressLint("Range")
    public static ArrayList<RingtoneModel> queryRingtoneNew(Context context) {
        if (Macros.DEBUG) {
            return testRingtoneNewList();
        } else {
            ArrayList<RingtoneModel> arrayList = new ArrayList<>();
            ContentResolver resolver = context.getContentResolver();
            Uri uri = Uri.parse(MetaData.DB_GOPLAY_CONTENT_QUERY + "/" + 0);
            Cursor cursor = resolver.query(uri, null, null, null, null);
            if (cursor == null) {
                Log.d(TAG, "queryRingtoneNew: cursor == null");
                return arrayList;
            }
            if (!cursor.moveToFirst()) {
                Log.d(TAG, "queryRingtoneNew: cursor.moveToFirst() == false");
                cursor.close();
                return arrayList;
            }
            Log.d(TAG, "queryRingtoneNew: db ok");
            do {
                RingtoneModel bean = new RingtoneModel();
                bean._id = cursor.getInt(cursor.getColumnIndex("_id"));
                bean.title = cursor.getString(cursor.getColumnIndex("title"));
                bean.status = cursor.getString(cursor.getColumnIndex("status"));
                bean.reason = cursor.getString(cursor.getColumnIndex("reason"));
                Log.d(TAG, "queryRingtoneNew: title: " + bean.title + " status: " + bean.status + " reason:" + bean.reason);
                arrayList.add(bean);
            } while (cursor.moveToNext());
            cursor.close();
            Log.d(TAG, "queryRingtoneNew: " + arrayList.size());
            return arrayList;
        }
    }

    public static void modifyRingtoneStatusDownloading(Context context) {
        ArrayList<RingtoneModel> arrayList = queryRingtoneNew(context);
        for (RingtoneModel model : arrayList) {
            if (model.status.equals("4"))
                continue;
            ContentValues values = new ContentValues();
            values.put("status", "1");
            Uri uri = Uri.parse(DB_GOPLAY_RINGTONES + "/" + model._id);
            int row = context.getContentResolver().update(uri, values, null, null);
            Log.d(TAG, "modifyRingtoneStatusDownloading: " + row);
        }
    }

    public static ArrayList<RingtoneModel> testRingtoneNewList() {
        ArrayList<RingtoneModel> arrayList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            RingtoneModel bean = new RingtoneModel();
            bean.title = "ringtone_" + i;
            bean.status = "5";
            bean.reason = "3";
            arrayList.add(bean);
        }
        return arrayList;
    }

    public static int addWatchFace(Context context, String name) {
        Log.d(TAG, "addWatchFaceConfig: ");
        Uri insertUri = Uri.parse(DB_GOPLAY_RINGTONES);
        ContentValues values = new ContentValues();
        values.put("contentType", "Watchface");
        values.put("title", name);
        values.put("status", 4);
        values.put("reason", 0);
        values.put("cid", 0);
        values.put("size", 0);
        values.put("url", "");
        values.put("hint", 0);
        Uri returnUri = context.getContentResolver().insert(insertUri, values);
        Log.d(TAG, "addWatchFaceConfig: " + returnUri.toString());
        String s = getLastPart(returnUri.toString());
        return Integer.parseInt(s);
    }

    public static String getLastPart(String url) {
        if (url.isEmpty())
            return "";
        return url.substring(url.lastIndexOf("/") + 1);
    }

    public static ArrayList<ApnModel> getCustomApnList(Context context) {
        if (Macros.DEBUG) {
            return new ArrayList<>();
        } else {
            ArrayList<ApnModel> arrayList = new ArrayList<>();
            ContentResolver resolver = context.getContentResolver();
            Uri uri = Uri.parse(DB_GOPLAY_APN);
            Cursor cursor = resolver.query(uri, null, null, null, null);
            if (cursor == null) {
                Log.d(TAG, "getCustomApnList: cursor == null");
                return arrayList;
            }
            if (!cursor.moveToFirst()) {
                Log.d(TAG, "getCustomApnList: cursor.moveToFirst() == false");
                cursor.close();
                return arrayList;
            }
            do {
                ApnModel bean = new ApnModel();
                bean.apn_id = cursor.getString(cursor.getColumnIndex("apn_id"));
                bean._id = cursor.getString(cursor.getColumnIndex("_id"));
                Log.d(TAG, "getCustomApnList: apn_id: " + bean.apn_id + " _id:" + bean._id);
                arrayList.add(bean);
            } while (cursor.moveToNext());
            cursor.close();
            Log.d(TAG, "getCustomApnList: " + arrayList.size());
            return arrayList;
        }
    }

    public static int insertAPN(Context context, String value) {
        Log.d(TAG, "====== insertAPN ========: " + value);
        if (Macros.DEBUG) {
            return -1;
        } else {
            Uri uri = Uri.parse(DB_GOPLAY_APN);
            ContentValues values = new ContentValues();
            values.put("_id", value);
            Uri returnUri = context.getContentResolver().insert(uri, values);
            Log.d(TAG, "insertAPN row: " + returnUri.toString());
            String s = getLastPart(returnUri.toString());
            return Integer.parseInt(s);
        }
    }

    public static void updateAPN(Context context, ApnModel model) {
        Log.d(TAG, "====== updateAPN =======: " + model._id);
    }

    public static void deleteAPN(Context context, ApnModel model) {
        Log.d(TAG, "====== deleteAPN =======: " + model.apn_id);
        if (Macros.DEBUG) {

        } else {
            Uri uri = Uri.parse(DB_GOPLAY_APN + "/" + model.apn_id);
            int row = context.getContentResolver().delete(uri, null, null);
            Log.d(TAG, "deleteAPN row: " + row);
        }
    }
}
