package com.hh.lib_device.view;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;


import com.hh.lib_base.utils.DoubClickUtils;
import com.hh.lib_base.utils.ImageLoaderUtils;
import com.hh.lib_device.R;
import com.hh.lib_device.bean.ExitAppletEvent;

import org.greenrobot.eventbus.EventBus;


/**
 * @author xwh
 * time 2024/8/27
 * Description: 悬浮返回键
 **/
public class TouchBackView extends LinearLayout {

    private String appId = "";


    private WindowManager EnterWindowManager;//本地过度页加载


    public TouchBackView(Context context, String appid) {
        super(context);
        this.appId = appid;
        init(context);
    }


    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_tocch_back_view, this);
        EnterWindowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        WindowManagerInitView(context, this);
        int type;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;// 系统提示window
        }
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,

                type,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
         /*               | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN//将window放置在整个屏幕之内,无视其他的装饰(比如状态栏)
                        | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS//允许window扩展值屏幕之外
                        | WindowManager.LayoutParams.FLAG_FULLSCREEN//当这个window显示的时候,隐藏所有的装饰物(比如状态栏)这个flag允许window使用整个屏幕区域
                        | WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER,//标记在其它窗口的LayoutParams.flags中的存在情况而不断地被调整*/
                PixelFormat.TRANSPARENT
        );
        layoutParams.gravity = Gravity.TOP | Gravity.RIGHT;

        //展示悬浮view
        EnterWindowManager.addView(this, layoutParams);
        setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (DoubClickUtils.onDoubClick()) {
                    return;
                }
                EventBus.getDefault().post(new ExitAppletEvent(appId));
                release();
            }
        });
    }


    private void WindowManagerInitView(Context context, View view) {
        ImageLoaderUtils.Companion.loadRes(context==null?getContext():context.getApplicationContext(),
                R.mipmap.ic_touch_back, view.findViewById(R.id.iv_back));
    }

    public void release() {
        try {
            if (EnterWindowManager != null) {
                EnterWindowManager.removeView(this);
                EnterWindowManager = null;
            } else {
                EnterWindowManager = (WindowManager) getContext().getApplicationContext()
                        .getSystemService(Context.WINDOW_SERVICE);
                if (EnterWindowManager != null) {
                    EnterWindowManager.removeView(this);
                    EnterWindowManager = null;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
