package com.example;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import com.example.plugina.PluginView;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.plugina.databinding.ActivityMainBinding;

import com.bingo.apkloader.base.ApkContext;
import com.bingo.apkloader.base.ApkWindowBuild;

import com.example.plugina.R;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button showButton;
    private Button showButton2;
    private Button hideButton;
    PluginView pluginView;
//    private LinearLayout pluginView;
//    private TextView pluginTitle;
//    private ViewGroup.LayoutParams windowParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

//        showButton = findViewById(R.id.show);
//        showButton.setOnClickListener(this);
//        showButton2 = findViewById(R.id.show2);
//        showButton2.setOnClickListener(this);
//        hideButton = findViewById(R.id.hide);
//        hideButton.setOnClickListener(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getPackageName()));
//        startActivityForResult(intent);
                startActivity(intent);
            }
        }

//        pluginView.show();
//        pluginView = findViewById(R.id.plugin_view);
//        pluginTitle = findViewById(R.id.plugin_title);
//
//        windowParams = pluginView.getLayoutParams();
    }

    @Override
    public void onClick(View v) {
//        if (v.getId() == R.id.show){
//            WindowManager ap =  (WindowManager) this.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
//            Map<String, Object> params = Map.of(
//                    "width", "40%",
//                    "height", 480,
//                    "direction", "left", // "left", "right", "top", "bottom"
//                    "title", "Plugin Title"
//            );
//            pluginView = new PluginView(this, ap, params);
//            // 获取屏幕宽度
//            pluginView.show();
//        } else if (v.getId() == R.id.show2){
//            WindowManager ap =  (WindowManager) this.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
//            Map<String, Object> params = Map.of(
//                    "width", "40%",
//                    "height", 480,
//                    "direction", "right", // "left", "right", "top", "bottom"
//                    "title", "Plugin Title"
//            );
//            pluginView = new PluginView(this, ap, params);
//            // 获取屏幕宽度
//            pluginView.show();
//        } else {
//            pluginView.hide();
//        }
    }



    @Override
    protected void onResume() {
        super.onResume();
        // Activity 可见时显示浮动窗口
        if (pluginView != null) {
            pluginView.show();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Activity 不可见时隐藏浮动窗口
        if (pluginView != null) {
            pluginView.hide();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 清理资源
        if (pluginView != null) {
            pluginView.hide();
        }
    }

//    public void show(Map<String, Object> params) {
//        parseParams(params);
//
////        TextView titleView = contentView.findViewById(R.id.plugin_title);
////        titleView.setText(this.title);
//
//        if ("horizontal".equals(this.orientation)){
//            int maxScreenWidth = windowManager.getDefaultDisplay().getWidth();
//            windowParams.width = (int)(maxScreenWidth * 0.5);
//            windowParams.x = windowParams.width;
//        }else{
//            int maxScreenHeight = windowManager.getDefaultDisplay().getHeight();
//            windowParams.height = (int)(maxScreenHeight * 0.5);
//            windowParams.y = windowParams.height;
//        }
//        windowParams.width = this.mWidth;
//        windowParams.height = this.mHeight;
//
//        // 设置初始位置在右侧外部
//        windowParams.x = windowManager.getDefaultDisplay().getWidth(); // 初始在右侧外面
//        windowParams.y = 0;
//
//        Log.d(TAG, "-->show this.apkWindowBuild.AddView");
//        this.apkWindowBuild.AddView(this.contentView, windowParams);
//
//
//        // 设置动画（从右侧滑入）
//        this.contentView.animate()
//                .translationX(0)  // 动画移动到屏幕左侧
//                .setDuration(300)
//                .start();
//    }
//
//    // 隐藏插件信息框
//    public void hide() {
//        if (this.contentView != null) {
//            this.contentView.animate()
//                    .translationX(this.contentView.getWidth())  // 滑回右侧
//                    .setDuration(300)
//                    .withEndAction(() -> {
//                        windowManager.removeView(this.contentView);  // 从 WindowManager 移除
//                        this.contentView = null;
//                    })
//                    .start();
//        }
//    }
//
//    private void init() {
//        if (windowManager == null) {
//            windowManager = (WindowManager) this.apkContext.getContext().getSystemService(Context.WINDOW_SERVICE);
//        }
//        if (windowParams == null) {
//            // 初始化LayoutParams
//            windowParams = new WindowManager.LayoutParams(
//                    WindowManager.LayoutParams.MATCH_PARENT,
//                    WindowManager.LayoutParams.MATCH_PARENT,
//                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
//                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
//                    PixelFormat.TRANSLUCENT
//            );
//        }
//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
//            windowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
//        }else{
//            windowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
//        }
//        windowParams.gravity = Gravity.START | Gravity.TOP;
//    }
//
//
//    private void parseParams(Map<String, Object> params) {
//        if (params != null) {
//            this.mWidth = (int) params.getOrDefault("width", 640);
//            this.mHeight = (int) params.getOrDefault("height", 480);
//            this.orientation = (String) params.getOrDefault("orientation", "vertical");
//            this.title = (String) params.getOrDefault("title", "");
//        } else {
//            this.mWidth = 640;
//            this.mHeight = 480;
//            this.orientation = "vertical"; // 默认竖屏
//            this.title = ""; // 默认标题
//        }
//    }
}