package com.genlot.nativesdk.sender;

import android.util.Log;

import com.genlot.nativesdk.SystemNI;
import com.genlot.nativesdk.model.Version;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class TCPDownload {
    private static final String SERVER_APK = "system/LT-MS03W/MS03W.apk";
    private static final String SERVER_VERSION = "system/LT-MS03W/version";
    /**
     * 服务器站点号路径 相对路径
     * 绝对路径 /home/nxfepv11/dat/Download/terms/stataddr/
     */
    private static final String SERVER_STATION_ADDRESS = "terms/stataddr/stataddr.";
    public static final String SERVER_NOTICE = "common/notice";
    public static final String SERVER_HISTORY_NOTICE = "history/notice/notice.";
    public static final String SERVER_BULLETIN = "common/winbulletin00";
    public static final String SERVER_HISTORY_BULLETIN = "history/winbulletin/winbulletin00";

    public static final String LOCAL_STATION_ADDRESS = "/address";
    private static final String LOCAL_VERSION = "/version";
    public static final String LOCAL_NOTICE = "/notice.txt";

    private static final String VERSION_NAME = "name";
    private static final String VERSION_CODE = "code";

    public static void downloadApp(final OnDownloadCompletedListener listener) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                String localFile = SystemNI.getDownloadPath() + "/ms01.apk";
                int result = SystemNI.downloadFile(SERVER_APK, localFile, 1);
                if (listener != null) {
                    listener.onDownloadCompleted(result, localFile);
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

    /**
     * 下载站地址
     *
     * @param listener  OnDownloadCompletedListener
     */
    public static void downloadStationAddress(final OnDownloadCompletedListener listener) {
        Runnable runnable = () -> {
            String localFile = SystemNI.getDownloadPath() + LOCAL_STATION_ADDRESS;
            String serverFile = SERVER_STATION_ADDRESS + SystemNI.getTerminalID();
            int result = SystemNI.downloadFile(serverFile, localFile, 1);
            if (listener != null) {
                listener.onDownloadCompleted(result, localFile);
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

    /**
     * 下载文件
     */
    public static int downloadFile(String serverPath, String localPath) {
        return SystemNI.downloadFile(serverPath, localPath, 0);
    }

    /**
     * 读取下载文件内容
     * @param localFile 本地下载文件
     * @return  String
     */
    public static String readDownloadFile(String localFile) {
        StringBuilder content = new StringBuilder();
        File file = new File(localFile);
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")));
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }

    public static Version downloadVersion() {
        Version version = new Version();
        String localFile = SystemNI.getDownloadPath() + LOCAL_VERSION;
        int result = SystemNI.downloadFile(SERVER_VERSION, localFile, 1);
        if (result == 0) {
            File file = new File(localFile);
            BufferedReader reader;
            try {
                reader = new BufferedReader(new FileReader(file));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.trim().startsWith(VERSION_NAME)) {
                        version.name = line.split(":")[1];
                    }
                    if (line.trim().startsWith(VERSION_CODE)) {
                        version.code = Integer.parseInt(line.split(":")[1]);
                    }
                }
                reader.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return version;
    }


    public interface OnDownloadCompletedListener {
        void onDownloadCompleted(int result, String fileName);
    }
}
