package com.xixun.xixunplayer;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.view.Gravity;
import android.widget.Toast;

import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Vector;

import gnph.util.IOs;
import gnph.util.JSMap;

public class Util {
    public static JSMap cfg;
    public static int screenWidth = 320, screenHeight = 160;

    public static void initDir(Context ctx) {
        var dir = Build.VERSION.SDK_INT < Build.VERSION_CODES.R ? Environment.getExternalStorageDirectory().getAbsolutePath() + "/XixunPlayer" : ctx.getExternalFilesDir(null).getAbsolutePath();
        Util.println("\n---- Init dir "+dir);
        Util.programDir = dir + "/program";
        Util.backImgFile = dir + "/background";

        var program = new File(Util.programDir);
        if(program.isFile()) program.delete();
        Util.println("  mkdir: "+program.mkdirs());
    }

    public static final BitmapFactory.Options noScaled = new BitmapFactory.Options();
    static {
        noScaled.inScaled = false;
    }
    public static final Vector<TCPThread> socketThreads = new Vector<>();

    public static StringBuffer buf = new StringBuffer();
    public static SimpleDateFormat dateFmt = new SimpleDateFormat("yy-MM-dd HH:mm:ss.SSS");

    public static void println(String msg) {
        System.out.println(msg);
        if(buf.length()>1000000) buf.replace(0, 100000, "");
        buf.append(msg).append("\n");
    }
    public static void printStackTrace(Throwable e) {
        println(toStackTrace(e));
    }

    public static String toStr(Throwable e) {
        var traces = e.getStackTrace();
        var msg = e.toString();
        for(var trace : traces) if(trace.getClassName().startsWith("com.xixun.xixunplayer.")) {
            msg += "\n  at "+trace.getFileName()+'.'+trace.getMethodName()+':'+trace.getLineNumber();
        }
        return msg;
    }
    public static String toStackTrace(Throwable e) {
        var out = new CharArrayWriter();
        var writer = new PrintWriter(out);
        e.printStackTrace(writer);
        writer.flush();
        return out.toString();
    }

    public static Toast makeText(Context context, CharSequence text) {
        var toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
        return toast;
    }

    public static String programDir, backImgFile;
    public static String getCardId() {
        try {
            var bytes = IOs.readBytesClose(new FileInputStream("/data/joey/signed/card.id"));
            if(bytes.length < 40) return "";
            byte[] cMyKey = new byte[]{97, 119, 38, 3, 46, 112, 36, 93, 58, 100, 103, 62, 115, 112, 114, 51, 43, 61, 2, 101, 119};
            for(int i=0; i<20; ++i) bytes[i] = (byte) (bytes[i * 2] - cMyKey[i] - i - (bytes[i * 2 + 1] - 3));
            var cardId = new String(bytes);
            if(cardId.length() > 13) cardId = cardId.substring(0, 13);
            return cardId;
        } catch (IOException e) {
            printStackTrace(e);
            return "";
        }
    }
}
