package de.j4velin.chess.util;

import android.database.Cursor;

import java.io.FileWriter;
import java.util.Date;

import de.j4velin.chess.BuildConfig;

/*
 * Copyright 2014 Thomas Hoffmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class Logger {

    private static FileWriter fw;
    private static final Date date = new Date();
    private final static String APP = "Chess";

    public static void log(final Throwable ex) {
        if (!BuildConfig.DEBUG) return;
        log(ex.getClass().getName());
        log(ex.getMessage());
        for (StackTraceElement ste : ex.getStackTrace()) {
            log(ste.toString());
        }
    }

    public static void log(final Cursor c) {
        if (!BuildConfig.DEBUG) return;
        c.moveToFirst();
        String title = "";
        for (int i = 0; i < c.getColumnCount(); i++)
            title += c.getColumnName(i) + " | ";
        log(title);
        while (!c.isAfterLast()) {
            title = "";
            for (int i = 0; i < c.getColumnCount(); i++)
                title += c.getString(i) + " | ";
            log(title);
            c.moveToNext();
        }
        c.close();
    }

    @SuppressWarnings("deprecation")
    public static void log(String msg) {
        if (!BuildConfig.DEBUG) return;
        if (msg != null) android.util.Log.d(APP, msg);
        //        try {
        //            if (fw == null) {
        //                fw = new FileWriter(new File(
        //                        Environment.getExternalStorageDirectory().toString() + "/" + APP + ".log"),
        //                        true);
        //            }
        //            date.setTime(System.currentTimeMillis());
        //            fw.write(date.toLocaleString() + " - " + msg + "\n");
        //            fw.flush();
        //        } catch (IOException e) {
        //            e.printStackTrace();
        //        }
    }

    protected void finalize() throws Throwable {
        try {
            if (fw != null) fw.close();
        } finally {
            super.finalize();
        }
    }

}