package com.xugame.gameconsole.util;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.xugame.gameconsole.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

public class CheckFileCRC implements Runnable {
    private static final String TAG = "CheckFileCRCTAG";
    private Thread mThread;
    private Handler mHandler;
    private CheckFileCRCListener mListener;
    private String mPath;
    private String mCoreName;
    private Context mContext;

    public CheckFileCRC(Context context, CheckFileCRCListener listener) {
        this.mContext = context;
        this.mListener = listener;
    }

    public void check(String path) {
        DebugUtil.i(TAG,"check"+path);
        if (TextUtils.isEmpty(path)) {
            this.mListener.onCheckCoresCRCError(-1);
            return;
        }
        mPath = path;
        File file = new File(path);
        if (!file.exists()) {
            this.mListener.onCheckCoresCRCError(-2);
            return;
        }
        mCoreName = file.getName();
        this.init();

        this.mThread = new Thread(this);
        this.mThread.setPriority(Thread.MAX_PRIORITY);
        this.mThread.start();
    }

    private void init() {
        mHandler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if(msg.what==0){
                    Bundle bundle=msg.getData();
                    if(bundle!=null){
                        long filecrc=bundle.getLong("filecrc");
                        String dfCrc=bundle.getString("default_crc");
                        if(mListener!=null){
                            mListener.onCheckCoresCRCDone(""+filecrc,dfCrc);
                        }
                    }
                }
            }
        };
    }

    @Override
    public void run() {
        long filecrc = ChecksumCRC32(mPath);
        String defaultCrc = returnDEF(mCoreName);
        DebugUtil.i(TAG,"checkCrc="+filecrc+"defCrc="+defaultCrc);
        if(mHandler!=null){
            Message message=new Message();
            message.what=0;
            Bundle bundle=new Bundle();
            bundle.putLong("filecrc",filecrc);
            bundle.putString("default_crc",defaultCrc);
            message.setData(bundle);
            mHandler.sendMessage(message);
        }
    }


    public long ChecksumCRC32(String path) {
        long checksum = 0;

        CheckedInputStream cis = null;
        File file = new File(path);

        try {
            cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
            byte[] buf = new byte[1024];

            while (cis.read(buf) >= 0) {
                checksum = cis.getChecksum().getValue();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return checksum;
    }

    private String returnDEF(String name) {
        if (name.equals("fbneo_libretro_android.so")) {
            return mContext.getResources().getString(R.string.fbneo_libretro_android);
        } else if (name.equals("fceumm_libretro_android.so")) {
            return mContext.getResources().getString(R.string.fceumm_libretro_android);
        } else if (name.equals("mame2010_libretro_android.so")) {
            return mContext.getResources().getString(R.string.mame2010_libretro_android);
        } else if (name.equals("snes9x2010_libretro_android.so")) {
            return mContext.getResources().getString(R.string.snes9x2010_libretro_android);
        } else if (name.equals("mgba_libretro_android.so")) {
            return mContext.getResources().getString(R.string.mgba_libretro_android);
        } else if (name.equals("tgbdual_libretro_android.so")) {
            return mContext.getResources().getString(R.string.tgbdual_libretro_android);
        } else if (name.equals("genesis_plus_gx_wide_libretro_android.so")) {
            return mContext.getResources().getString(R.string.genesis_plus_gx_wide_libretro_android);
        } else if (name.equals("mednafen_ngp_libretro_android.so")) {
            return mContext.getResources().getString(R.string.mednafen_ngp_libretro_android);
        } else if (name.equals("mednafen_pce_libretro_android.so")) {
            return mContext.getResources().getString(R.string.mednafen_pce_libretro_android);
        } else if (name.equals("pcsx_rearmed_libretro_android.so")) {
            return mContext.getResources().getString(R.string.pcsx_rearmed_libretro_android);
        } else if (name.equals("smsplus_libretro_android.so")) {
            return mContext.getResources().getString(R.string.smsplus_libretro_android);
        } else if (name.equals("mednafen_wswan_libretro_android.so")) {
            return mContext.getResources().getString(R.string.mednafen_wswan_libretro_android);
        } else if (name.equals("ppsspp_libretro_android.so")) {
            return mContext.getResources().getString(R.string.ppsspp_libretro_android);
        } else if (name.equals("mupen64plus_next_gles2_libretro_android.so")) {
            return mContext.getResources().getString(R.string.mupen64plus_next_gles2_libretro_android);
        } else if (name.equals("flycast_libretro_android.so")) {
            return mContext.getResources().getString(R.string.flycast_libretro_android);
        } else if (name.equals("stella_libretro_android.so")) {
            return mContext.getResources().getString(R.string.stella_libretro_android);
        } else if (name.equals("vice_x64_libretro_android.so")) {
            return mContext.getResources().getString(R.string.vice_x64_libretro_android);
        } else if (name.equals("picodrive_libretro_android.so")) {
            return mContext.getResources().getString(R.string.picodrive_libretro_android);
        }  else {
            return "";
        }
    }

}
