package com.qimai.log;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.channels.FileChannel;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DBCopyUtils {


    private static final String TAG = "DBCopyUtils";
   /* public static void copyLogToFTP(String tag) {
        File file = null;
        final String SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();

        final String logPath =  LogInit.Companion.getInstance().mApplication.getFilesDir().toString() + "/release/xlog";
                 file = new File(logPath);
        try {
            // File  logFile =
            if (!file.exists()) {
                return;
            }
            File[] fileList = file.listFiles();
            if (fileList != null && fileList.length > 0) {
                for (int i = 0; i < fileList.length; i++) {
                    file = fileList[i];
                    Log.d(TAG, "copylogToFTP: file = " + file.getAbsolutePath().toString());
                    final FileInputStream in = new FileInputStream(file);
                    final File finalFile = file;
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            boolean isUpload = FtpUtil.uploadFile("119.29.25.121", 221, "dataftp"
                                    , "Data2019@#%", "/", tag, finalFile.getName(), in);
                            Log.d(TAG, "copyDataBaseToSD: isUpload= " + isUpload + "DeviceUtils.getDeviceSN()= " + tag);

                            if (isUpload) {

                                finalFile.delete();

                            }

                        }
                    }).start();
                }
            } else {

                return;
            }

        } catch (Exception e) {
            Log.d(TAG, "copyDataBaseToSD: failed= " + e.getMessage());

            //LogUtils.e(TAG, "copy dataBase to SD error.");
            e.printStackTrace();
        } finally {

        }


    }*/

    /**
     * @param file 代表文件不是目录
     **/

    public static void copyLogToFTP(File file,String tag) {

        FileInputStream in = null;
        try {
            // File  logFile =
            if (!file.exists()) {
                return;
            }

            in = new FileInputStream(file);

            Log.d(TAG, "copyLogToFTP: thread= " + Thread.currentThread().getName());
            boolean isUpload = FtpUtil.uploadFile("119.29.25.121", 221, "dataftp"
                    , "Data2019@#%",
                    "/", tag, file.getName(), in);

            Log.d(TAG, "copyLogToFTP: isUpload= " + isUpload);


            /*File []fileList = file.listFiles();
            if (fileList!=null&&fileList.length>0){
                for (int i = 0; i <fileList.length;i++) {
                    file = fileList[i];
                    Log.d(TAG, "copylogToFTP: file = "+file.getAbsolutePath().toString());
                    FileInputStream in = new FileInputStream(file);
                    File finalFile = file;
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            boolean isUpload = FtpUtil.uploadFile("119.29.25.121",221,"dataftp"
                                    ,"Data2018@#%",
                                    "/",DeviceUtils.getDeviceSN(), finalFile.getName(),in);
                            Log.d(TAG, "copyDataBaseToSD: isUpload= "+isUpload+"DeviceUtils.getDeviceSN()= "+(DeviceUtils.getDeviceSN()));

                            if (isUpload){
                                finalFile.delete();

                            }

                        }
                    }).start();
                }
            }
            else{

                return;
            }

        }
        catch (Exception e) {
            Log.d(TAG, "copyDataBaseToSD: failed= "+e.getMessage());

            //LogUtils.e(TAG, "copy dataBase to SD error.");
            e.printStackTrace();
        }finally{

        }

*/


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }

    }

}
