package com.example.plugina;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bingo.apkloader.base.ApkContext;
import com.bingo.apkloader.base.ApkWindowBuild;
import com.github.faucamp.simplertmp.RtmpHandler;

import net.ossrs.yasea.SrsCameraView;
import net.ossrs.yasea.SrsEncodeHandler;
import net.ossrs.yasea.SrsPublisher;
import net.ossrs.yasea.SrsRecordHandler;

import java.util.Map;

public class PluginView implements View.OnClickListener {
    private static final String TAG = "PluginView";

    private ApkWindowBuild apkWindowBuild;
    private ApkContext apkContext;
//    private Context context;
    private WindowManager windowManager;
    private WindowManager.LayoutParams windowParams;

    private String title = "";
    private int mWidth = 640;
    private String direction = "left"; // 默认右侧

    private View pluginView = null;
    private FrameLayout pluginContent = null;
    private ImageView btnClose = null;
    private ImageView btnBack = null;

    public PluginView(ApkContext apkContext, ApkWindowBuild apkWindowBuild, Map<String, Object> params) {
        this.apkContext = apkContext;
        this.apkWindowBuild = apkWindowBuild;

        this.pluginView = this.apkWindowBuild.Inflate(R.layout.plugin_view);

        this.init(params);
    }

//    public PluginView(Context context, WindowManager windowManager, Map<String, Object> params) {
//        this.context = context;
//        this.windowManager = windowManager;
//
//        // 通过 LayoutInflater 加载你的布局文件
//        LayoutInflater inflater = LayoutInflater.from(context);
//        this.pluginView = inflater.inflate(R.layout.plugin_view, null);
//
//        this.init(params);
//    }

    public void show() {
        Log.d(TAG, "-->show this.apkWindowBuild.AddView");
        if (this.pluginView.getParent() != null) {
            this.apkWindowBuild.updateViewLayout(pluginView, windowParams);
        } else {
            this.apkWindowBuild.AddView(this.pluginView, windowParams);
        }
    }

    public void updatePluginView(View view) {
        this.pluginContent.removeAllViews();
        this.pluginContent.addView(view);
    }

    // 隐藏插件信息框
    public void hide() {
        // 检查pluginView是否已经被移除
        if (this.pluginView.getParent() != null) {
            this.apkWindowBuild.removeView(this.pluginView);
        } else {
            Log.d(TAG, "--> pluginView is already removed, skipping remove operation.");
        }
    }

    private void init(Map<String, Object> params) {
        pluginContent = pluginView.findViewById(R.id.plugin_content);
        btnClose = pluginView.findViewById(R.id.plugin_close_btn);
        btnClose.setOnClickListener(this);
        btnBack = pluginView.findViewById(R.id.plugin_back_btn);
        btnBack.setOnClickListener(this);
        TextView titleView = pluginView.findViewById(R.id.plugin_title);

        if (windowManager == null) {
            windowManager = (WindowManager) this.apkContext.getContext().getSystemService(Context.WINDOW_SERVICE);
        }
        if (windowParams == null) {
            // 初始化LayoutParams
            windowParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    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;
        }

        int screenWidth = windowManager.getDefaultDisplay().getWidth();
        int screenHeight = windowManager.getDefaultDisplay().getHeight();
        int defaultWidth = (int) (screenWidth * 0.4);
        if (params != null) {
            this.title = (String) params.getOrDefault("title", "");
            this.mWidth = parseSize(params.get("width"), screenWidth, defaultWidth);
            this.direction = (String) params.getOrDefault("direction", "left");
        } else {
            this.title = ""; // 默认标题
            this.mWidth = defaultWidth;
            this.direction = "left"; // 默认竖屏
        }
        this.title += "a";

        windowParams.x = 200;  // 设置x坐标，距离屏幕
        windowParams.y = 0; // 设置y坐标，距离屏幕顶部15px
        windowParams.width = this.mWidth;  // 设置宽度
        windowParams.height = screenHeight;  // 设置高度，减去顶部和底部的间距

        if (this.direction.equals("left")) {
            windowParams.gravity = Gravity.START | Gravity.TOP;  // 使用 LEFT
        } else if (this.direction.equals("right")) {
            windowParams.gravity = Gravity.END | Gravity.TOP;  // 使用 RIGHT
        }
//        windowParams.x = 0; // 设置x坐标，距离屏幕左边0px

        titleView.setText(this.title);
    }

    // 获取状态栏高度
    private int getStatusBarHeight() {
        int statusBarHeight = 0;
        Resources resources = this.apkContext.getContext().getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");

        if (resourceId > 0) {
            statusBarHeight = resources.getDimensionPixelSize(resourceId);
        }

        return statusBarHeight;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.plugin_back_btn){
//            this.hide();
        } else if (v.getId() == R.id.plugin_close_btn) {
            this.hide();
        }
    }

    private int parseSize(Object value, int base, int defaultValue) {
        if (value == null) return defaultValue;
        String str = value.toString().trim();
        try {
            if (str.endsWith("%")) {
                float percent = Float.parseFloat(str.replace("%", "")) / 100f;
                return (int) (base * percent);
            } else {
                return Integer.parseInt(str);
            }
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
}
