package com.tsi.navi.database;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class DbHelper extends SQLiteOpenHelper {
    private final static String TAG = DbHelper.class.getSimpleName();

    private static String DATABASE_PATH = "";
    private static String DATABASE_NAME = "tsi-navi-node.db";
    private static String DATABASE_FILE_NAME = "";
    private final static int DATABASE_VERSION = 1;

    private SQLiteDatabase database;
    private final Context context;

    @SuppressLint("SdCardPath")
    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        this.context = context;
        DATABASE_FILE_NAME = getDatabaseFileName(context);
        //Log.d(TAG,"database_name: " + DATABASE_NAME);
        //Log.d(TAG,"database_path: " + DATABASE_PATH);
        //Log.d(TAG,"data_filename: " + DATABASE_FILE_NAME);
    }

    void createDatabase() throws IOException {
        if (!checkDatabase()) {
            this.getReadableDatabase();
            copyDatabase();
            this.close();
        }
    }

    private boolean checkDatabase() {
        File dbFile = new File(DATABASE_FILE_NAME);
        //Log.e(TAG, "checkDatabase: " + dbFile.exists());
        return dbFile.exists();
    }

    public static String getDatabaseFileName(Context context) {
        DATABASE_PATH = "/data/data/" + context.getPackageName() + "/databases/";
        return DATABASE_PATH + DATABASE_NAME;
    }
    public synchronized static void deleteDatabase(Context context) {
        DATABASE_FILE_NAME = getDatabaseFileName(context);

        File dbFile = new File(DATABASE_FILE_NAME);
        //Log.d(TAG,"deleteDatabase: " + dbFile.getAbsolutePath());
        if (dbFile.exists()) {
            //Log.d(TAG,"deleteDatabase: " + dbFile.getAbsolutePath());
            dbFile.delete();
        }
        //Log.d(TAG,"deleteDatabase!");
    }

    boolean openDatabase() throws SQLException {
        database = SQLiteDatabase.openDatabase(DATABASE_FILE_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY);
        return database != null;
    }

    @Override
    public synchronized void close() {
        if (database != null) {
            database.close();
        }
        SQLiteDatabase.releaseMemory();
        super.close();
        //Log.d(TAG,"close!");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG,"onCreate!");
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        //Toast.makeText(mContext,"onOpen()",Toast.LENGTH_SHORT).show();
        //Log.d(TAG,"onOpen!");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //Log.d(TAG,"onUpgrade!");
    }

    // assets database copy to local sqlite
    private void copyDatabase() {
        try {
            File folder = new File(DATABASE_PATH);
            if (!folder.exists()) {
                folder.mkdir();
                //Log.e(TAG, "folder.mkdir");
            }

            String outFileName = DATABASE_FILE_NAME;
            InputStream inputStream = context.getAssets().open(DATABASE_NAME);
            OutputStream outputStream = new FileOutputStream(outFileName);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer,0, len);
            }
            outputStream.flush();;
            outputStream.close();
            inputStream.close();
            //Log.i(TAG, "DatabaseCopy ok.");
        }
        catch (IOException e) {
            e.printStackTrace();
            //Log.e(TAG,"DatabaseCopy IOException: " + e.getMessage());
        }
    }
}
