package com.yutou.jianr_mg.views;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.yutou.jianr_mg.Application;
import com.yutou.jianr_mg.Data.AppData;
import com.yutou.jianr_mg.Interfaces.DownloadFileInerface;
import com.yutou.jianr_mg.Network.HttpUtils;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.Log;
import com.yutou.jianr_mg.Tools.Utils;

import Interfaces.BaseActivityInterface;
import dalvik.system.DexClassLoader;
import me.drakeet.materialdialog.MaterialDialog;

public class BaseActivity extends AppCompatActivity {
    private BaseActivityInterface activityInterface;
    private LinearLayout main_layut;
    private KProgressHUD hud;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        setTitle(R.string.app_name);
        hud=Utils.showLoading(this,"请稍后","正在下载");
        main_layut=findViewById(R.id.main_layut);
        String baseActivityName=getIntent().getStringExtra("activityName");
        String jar=getIntent().getStringExtra("jar");
        String name=getIntent().getStringExtra("name");
        String url=getIntent().getStringExtra("url");
        DexClassLoader dexClassLoader= AppData.plugsin.get(jar);

        try {
            System.out.println("jar:"+jar);
            System.out.println("BaseActivity:"+baseActivityName);
            activityInterface= (BaseActivityInterface) dexClassLoader.loadClass(baseActivityName).newInstance();
            activityInterface.onCreate(savedInstanceState,main_layut,this,getIntent());
        } catch (Exception e) {
            e.printStackTrace();
            activityInterface=new NotInter();
            MaterialDialog dialog=new MaterialDialog(this);
            dialog.setTitle("下载插件:"+name);
            dialog.setMessage("这可能是一个插件，但您未安装该插件，请问是否进行下载安装操作？");
            dialog.setNegativeButton("取消",view ->{dialog.dismiss();finish();});
            dialog.setPositiveButton("确定",view -> {
                Log.i("插件管理","下载插件:"+url);
                HttpUtils.downloadFile(url, Application.application.getFilesDir() + "/plugin/", new DownloadFileInerface() {
                    @Override
                    public boolean downloading(String fileName, int current, long length) {
                        return true;
                    }

                    @Override
                    public void over(boolean isOver) {
                        Toast.makeText(Application.application,"下载完成，重启应用生效",Toast.LENGTH_LONG).show();
                        hud.dismiss();
                        finish();
                    }
                });
               dialog.dismiss();
               hud.show();
            });
            dialog.show();
        }

    }

    @Override
    protected void onRestart() {
        activityInterface.onRestart();
        super.onRestart();
    }

    @Override
    protected void onPause() {
        activityInterface.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        activityInterface.onResume();
        super.onResume();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean tmp=activityInterface.onKeyDown(keyCode,event);
        if(tmp){
            return tmp;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        boolean tmp=activityInterface.onKeyUp(keyCode, event);
        if(tmp) {
            return tmp;
        }
        return super.onKeyUp(keyCode,event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean tmp=activityInterface.onTouchEvent(event);
        if(tmp)
            return tmp;
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDestroy() {
        activityInterface.onDestroy();
        super.onDestroy();
    }

    private class NotInter implements BaseActivityInterface{

        @Override
        public void onCreate(Bundle savedInstanceState, LinearLayout layout, Context context, Intent intent) {

        }

        @Override
        public void onRestart() {

        }

        @Override
        public void onResume() {

        }

        @Override
        public void onPause() {

        }

        @Override
        public void onDestroy() {

        }

        @Override
        public void getIntent(Intent intent) {

        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            return false;
        }

        @Override
        public boolean onKeyUp(int keyCode, KeyEvent event) {
            return false;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return false;
        }

        @Override
        public Data getData() {
            return null;
        }
    }
}
