package com.spbstu.appmath.Workout_Journal;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;



public class DBHelper extends SQLiteOpenHelper {

    public static final int DB_VERSION = 1;
    public static final String DB_NAME = "workout_journal.db";
    private String DB_PATH = null;
    private String DB_PATH_NAME = null;
    private final Context context;

    public DBHelper(final Context _context) {
        super(_context, DB_NAME, null, DB_VERSION);
        this.context = _context;
        DB_PATH = context.getFilesDir().getPath() + "/";
        DB_PATH_NAME = DB_PATH + DB_NAME;
    }

    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();
        if (!dbExist) {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        } else {
            return;
        }
    }

    private boolean checkDataBase(){
        SQLiteDatabase checkDB = null;
        try{
            checkDB = SQLiteDatabase.openDatabase(DB_PATH_NAME, null, SQLiteDatabase.OPEN_READONLY);
        }catch(SQLiteException e){
            //database does't exist yet.
        }
        if(checkDB != null){
            checkDB.close();
        }
        return checkDB != null;
    }

    private void copyDataBase() throws IOException {
        InputStream input = context.getAssets().open(DB_NAME);
        OutputStream output = new FileOutputStream(DB_PATH_NAME);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = input.read(buffer)) > 0){
            output.write(buffer, 0, length);
        }

        output.flush();
        output.close();
        input.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {}

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

    public List<Training> getAllPlannedTrainings() {
        final List<Training> trainings = new CopyOnWriteArrayList<>();
        SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH_NAME, null, SQLiteDatabase.OPEN_READONLY);
        Cursor res = db.rawQuery("SELECT * FROM '" + DBContract.WorkoutsPlan.TABLE + "'", null);
        res.moveToFirst();
        while(!res.isAfterLast()) {
            String name = res.getString(res.getColumnIndex(DBContract.WorkoutsPlan.COLUMN_NAME));
            trainings.add(new Training(name));
            res.moveToNext();
        }
        res.close();
        return trainings;
    }

    public List<Training> getAllDoneTrainings() {
        final List<Training> trainings = new CopyOnWriteArrayList<>();
        SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH_NAME, null, SQLiteDatabase.OPEN_READONLY);
        Cursor res = db.rawQuery("SELECT * FROM '" + DBContract.WorkoutsDone.TABLE + "'", null);
        res.moveToFirst();
        while(!res.isAfterLast()) {
            String name = res.getString(res.getColumnIndex(DBContract.WorkoutsDone.COLUMN_NAME));
            String date = res.getString(res.getColumnIndex(DBContract.WorkoutsDone.COLUMN_DATE));
            trainings.add(new Training(name, date));
            res.moveToNext();
        }
        res.close();
        return trainings;
    }

    public boolean deletePlannedTraining(final Training training) {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH_NAME, null, SQLiteDatabase.OPEN_READWRITE);
        if (training.date == null) {
            db.execSQL("DELETE FROM " + DBContract.WorkoutsPlan.TABLE + " WHERE " +
                    DBContract.WorkoutsPlan.COLUMN_NAME + " = " + training.name);
        }
        return false;
    }

    public boolean deleteDoneTraining(final Training training) {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH_NAME, null, SQLiteDatabase.OPEN_READWRITE);
        if (training.date != null) {
            db.execSQL("DELETE FROM " + DBContract.WorkoutsDone.TABLE + " WHERE " +
                    DBContract.WorkoutsDone.COLUMN_NAME + " = '" + training.name + "' AND " +
                    DBContract.WorkoutsDone.COLUMN_DATE + " = '" + training.date + "'");
        }
        return false;
    }
}

@@ -1075,24 +1075,26 @@
             
+//
 this.getRead
@@ -1274,45 +1274,8 @@
   %7D%0A
-        %7D else %7B%0A            return;%0A
     
@@ -3784,32 +3784,90 @@
 PEN_READWRITE);%0A
+        //SQLiteDatabase db = this.getWritableDatabase();%0A
         if (trai
@@ -3915,32 +3915,33 @@
 QL(%22DELETE FROM 
+'
 %22 + DBContract.W
@@ -3953,39 +3953,41 @@
 tsPlan.TABLE + %22
+'
  WHERE 
+'
 %22 +%0A            
@@ -4033,19 +4033,21 @@
 NAME + %22
+'
  = 
+'
 %22 + trai
@@ -4055,16 +4055,22 @@
 ing.name
+ + %22'%22
 );%0A     
@@ -4064,33 +4064,102 @@
 + %22'%22);%0A        
-%7D
+    db.close();%0A            return true;%0A        %7D%0A        db.close();
 %0A        return 
@@ -4418,16 +4418,17 @@
 TE FROM 
+'
 %22 + DBCo
@@ -4456,23 +4456,25 @@
 ABLE + %22
+'
  WHERE 
+'
 %22 +%0A    
@@ -4524,24 +4524,25 @@
 UMN_NAME + %22
+'
  = '%22 + trai
@@ -4560,16 +4560,17 @@
  %22' AND 
+'
 %22 +%0A    
@@ -4624,16 +4624,17 @@
 DATE + %22
+'
  = '%22 + 
@@ -4655,33 +4655,102 @@
 + %22'%22);%0A        
-%7D
+    db.close();%0A            return true;%0A        %7D%0A        db.close();
 %0A        return 

Delete training fixed