/*
 * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
 *
 * 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.
 */

package net.micode.notes.tool;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DateFormat;

import javax.naming.Context;

import android.database.Cursor;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns;

public class BackupUtils {
    private static final String TAG = "BackupUtils";
    // Singleton stuff
    private static BackupUtils sInstance;

    public static synchronized BackupUtils getInstance(Context context) {
        // synchronized"同步锁",作用是保证在同一时刻， 被修饰的代码块或方法只会有一个线程执行，以保证并发安全。这里是把这个方法锁住了
        // 运行到这个方法时,要检查一下有没有其它线程正在用这个方法(或者该类的其他同步方法)
        // (1)有:要等正在使用synchronized方法的线程运行完后再运行此线程(2)没有:锁定调用者,可以直接运行。

        if (sInstance == null) {// 判断如果当前备份为null,就新声明一个
            sInstance = new BackupUtils(context);
        }
        return sInstance;// 返回备份
    }

    /**
     * Following states are signs to represents backup or restore
     * status
     */
    // 表示此时SD卡有无被装入手机
    public static final int STATE_SD_CARD_UNMOUONTED = 0;
    // 表示备份文件是否存在
    public static final int STATE_BACKUP_FILE_NOT_EXIST = 1;
    // 表示数据此时是否被修改或者损坏
    public static final int STATE_DATA_DESTROIED = 2;
    // 表示某些导致恢复或备份失败的运行时异常
    public static final int STATE_SYSTEM_ERROR = 3;
    // 表示成功存储
    public static final int STATE_SUCCESS = 4;

    private TextExport mTextExport;

    private BackupUtils(Context context) {// 初始化函数
        mTextExport = new TextExport(context);
    }

    private static boolean externalStorageAvailable() {// 检测外部存储功能是否可用
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

    public int exportToText() {// 导出文本
        return mTextExport.exportToText();
    }

    public String getExportedTextFileName() {// 获取导出的文本文件名称
        return mTextExport.mFileName;
    }

    public String getExportedTextFileDir() {// 获取导出的文本文件的文件地址
        return mTextExport.mFileDirectory;
    }

    private static class TextExport {// 导出工具类
        private static final String[] NOTE_PROJECTION = { // 把NotesNolumn的一些拿作出为NOTE_PROJECTION
                NoteColumns.ID,
                NoteColumns.MODIFIED_DATE,
                NoteColumns.SNIPPET,
                NoteColumns.TYPE
        };

        private static final int NOTE_COLUMN_ID = 0;

        private static final int NOTE_COLUMN_MODIFIED_DATE = 1;

        private static final int NOTE_COLUMN_SNIPPET = 2;

        private static final String[] DATA_PROJECTION = { //// 把NotesNolumn的一些拿作出为DATA_PROJECTION
                DataColumns.CONTENT,
                DataColumns.MIME_TYPE,
                DataColumns.DATA1,
                DataColumns.DATA2,
                DataColumns.DATA3,
                DataColumns.DATA4,
        };

        private static final int DATA_COLUMN_CONTENT = 0;

        private static final int DATA_COLUMN_MIME_TYPE = 1;

        private static final int DATA_COLUMN_CALL_DATE = 2;

        private static final int DATA_COLUMN_PHONE_NUMBER = 4;

        private final String[] TEXT_FORMAT;
        private static final int FORMAT_FOLDER_NAME = 0;
        private static final int FORMAT_NOTE_DATE = 1;
        private static final int FORMAT_NOTE_CONTENT = 2;

        private Context mContext;
        private String mFileName;
        private String mFileDirectory;

        public TextExport(Context context) {
            TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);// 获取数据资源
            mContext = context;
            mFileName = "";// 设置文件名称
            mFileDirectory = "";// 设置导出文件地址
        }

        private String getFormat(int id) {// 获取文本的组成部分
            return TEXT_FORMAT[id];
        }

        /**
         * Export the folder identified by folder id to text
         */
        private void exportFolderToText(String folderId, PrintStream ps) {
            // 通过查询PARENT_ID等于folderId的note来选出制定ID文件夹下的Note，默认输出为ID升序
            Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI,
                    NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {
                            folderId
                    }, null);

            if (notesCursor != null) {
                if (notesCursor.moveToFirst()) {// 判断cursor是否移到了第一条上
                    do {
                        // 打印note最后一次更新的日期，ps里面保存有这份note的日期
                        ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
                                mContext.getString(R.string.format_datetime_mdhm),
                                notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
                        // Query data belong to this note
                        String noteId = notesCursor.getString(NOTE_COLUMN_ID);
                        exportNoteToText(noteId, ps);// 将文件导出到text
                    } while (notesCursor.moveToNext());// 使用moveToNext逐条读取
                }
                notesCursor.close();// 关闭notesCursor且释放内存
            }
        }

        /**
         * Export note identified by id to a print stream
         */
        private void exportNoteToText(String noteId, PrintStream ps) {
            Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, // 查询DataColumns的NOTE_ID等于传入的string型的noteId的note,默认为升序
                    DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
                            noteId
                    }, null);

            if (dataCursor != null) {// 利用光标来扫描内容，区别为callnote和note两种，靠ps.printline输出
                if (dataCursor.moveToFirst()) {// 从第一条开始遍历
                    do {
                        String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
                        if (DataConstants.CALL_NOTE.equals(mimeType)) {
                            // Print phone number
                            String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
                            long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE);
                            String location = dataCursor.getString(DATA_COLUMN_CONTENT);

                            if (!TextUtils.isEmpty(phoneNumber)) { // 判断是否为空字符，不为空则打印phoneNumber
                                ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), // 将phoneNumber格式化为和FORMAT_NOTE_CONTENT一样的格式后再打印
                                        phoneNumber));
                            }
                            // Print call date
                            ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat// 格式化为和FORMAT_NOTE_CONTENT一样的格式后再打印
                                    .format(mContext.getString(R.string.format_datetime_mdhm),
                                            callDate)));
                            // Print call attachment location
                            if (!TextUtils.isEmpty(location)) {// 判断location是否为空字符
                                ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), // 不为空则转换格式后打印location
                                        location));
                            }
                        } else if (DataConstants.NOTE.equals(mimeType)) {// 判断是否mimeType和NOTE一致
                            String content = dataCursor.getString(DATA_COLUMN_CONTENT);
                            if (!TextUtils.isEmpty(content)) {// 判断content是否为空字符
                                ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), // 不为空则转换格式和FORMAT_NOTE_CONTENT一致后打印content
                                        content));
                            }
                        }
                    } while (dataCursor.moveToNext());
                }
                dataCursor.close();// 关闭dataCursor且释放内存
            }
            // note之间打印line seperator
            try {
                ps.write(new byte[] {
                        Character.LINE_SEPARATOR, Character.LETTER_NUMBER
                });
            } catch (IOException e) {
                Log.e(TAG, e.toString());
            }
        }

        /**
         * Note will be exported as text which is user readable
         */
        public int exportToText() {// 通过调用上面的定义过的exportFolder和exportNote方法实现导出note为text
            if (!externalStorageAvailable()) {// 判断是否外部存储是被允许的
                Log.d(TAG, "Media was not mounted");// 不被允许就log提醒
                return STATE_SD_CARD_UNMOUONTED;// 返回sd卡为被装入手机,也就是返回0
            }

            PrintStream ps = getExportToTextPrintStream();
            if (ps == null) {// 判断ps是否为空
                Log.e(TAG, "get print stream error");// 为空则log提醒并返回STATE_SYSTEM_ERROR代表的数值，即返回3
                return STATE_SYSTEM_ERROR;
            }
            // First export folder and its notes
            Cursor folderCursor = mContext.getContentResolver().query(// 查询赋给folderCursor进行初始化
                    Notes.CONTENT_NOTE_URI,
                    NOTE_PROJECTION,
                    "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND "
                            + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR "
                            + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER,
                    null, null);

            if (folderCursor != null) {// 赋值成功则从第一个开始遍历
                if (folderCursor.moveToFirst()) {
                    do {
                        // 针对两种情况对folderName赋值
                        String folderName = "";
                        if (folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) {
                            folderName = mContext.getString(R.string.call_record_folder_name);
                        } else {
                            folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET);
                        }
                        if (!TextUtils.isEmpty(folderName)) {// 不为空则打印folderName
                            ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName));
                        }
                        String folderId = folderCursor.getString(NOTE_COLUMN_ID);
                        exportFolderToText(folderId, ps);// 以id为标识导出ps
                    } while (folderCursor.moveToNext());
                }
                folderCursor.close();
            }

            // 将根目录里的便签导出（由于不属于任何文件夹，因此无法通过文件夹导出来实现这一部分便签的导出）
            Cursor noteCursor = mContext.getContentResolver().query(// 查询PARENT_ID等于0且NoteColumns.TYPE等于Notes.TYPE_NOTE的结果赋给noteCursor
                    Notes.CONTENT_NOTE_URI,
                    NOTE_PROJECTION,
                    NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID
                            + "=0",
                    null, null);// 默认升序

            if (noteCursor != null) {
                if (noteCursor.moveToFirst()) {// 不为空则从第一个开始遍历
                    do {
                        ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
                                mContext.getString(R.string.format_datetime_mdhm),
                                noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
                        // Query data belong to this note
                        String noteId = noteCursor.getString(NOTE_COLUMN_ID);
                        exportNoteToText(noteId, ps);// 调用exportNoteToText进行导出
                    } while (noteCursor.moveToNext());
                }
                noteCursor.close();
            }
            ps.close();

            return STATE_SUCCESS;
        }

        /**
         * Get a print stream pointed to the file {@generateExportedTextFile}
         */
        private PrintStream getExportToTextPrintStream() {
            File file = generateFileMountedOnSDcard(mContext, R.string.file_path,
                    R.string.file_name_txt_format);
            if (file == null) {// file为空则产生file失败，log提醒后返回空
                Log.e(TAG, "create file to exported failed");
                return null;
            }
            mFileName = file.getName();// 调用则返回一个字符串值,该值是给定File对象的名称
            mFileDirectory = mContext.getString(R.string.file_path);// 得到文件地址，返回string
            PrintStream ps = null;
            try {
                FileOutputStream fos = new FileOutputStream(file);
                ps = new PrintStream(fos);// 给ps赋值
            } catch (FileNotFoundException e) {// 赋值失败，打开文件失败则将此可抛出对象及其回溯信息打印到标准错误流且返回空
                e.printStackTrace();
                return null;
            } catch (NullPointerException e) {// 空指针异常则将此可抛出对象及其回溯信息打印到标准错误流且返回空
                e.printStackTrace();
                return null;
            }
            return ps;
        }
    }

    /**
     * Generate the text file to store imported data
     */
    private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {// 产生一个文件并导出到SD卡上
        StringBuilder sb = new StringBuilder();
        sb.append(Environment.getExternalStorageDirectory());
        sb.append(context.getString(filePathResId));
        File filedir = new File(sb.toString());
        sb.append(context.getString(
                fileNameFormatResId,
                DateFormat.format(context.getString(R.string.format_date_ymd),
                        System.currentTimeMillis())));
        File file = new File(sb.toString());

        try {
            if (!filedir.exists()) {
                filedir.mkdir();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            return file;
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
