package com.hh.arome.secondaryscreen;


import android.annotation.SuppressLint;
import android.app.Presentation;
import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hh.arome.secondaryscreen.bean.ConfigBean;

import java.util.Base64;


/**
 * @Description: 副屏页面
 * @author: Xia WeiHao
 * @date: 2024/6/19
 */
public class SecondaryScreenPage extends Presentation implements View.OnClickListener{

    private ConfigBean mConfig;
    private static SecondaryScreenPage screenPage=null;
    private ImageView imageView;

    public SecondaryScreenPage(Context context, Display display, ConfigBean config){
        super(context,display);
        mConfig=config;
    }

    @SuppressLint({"MissingInflatedId", "LongLogTag"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_secondary_screen_page);
        imageView=findViewById(R.id.iv_bg);
        Glide.with(getContext())
                .load(mConfig.getPhotoBase64Str())
                //.apply(mRequestOptions)
                .centerCrop()
                .into(imageView);



    }

    public void updateConfig(ConfigBean config){
        if(imageView!=null){
            Glide.with(getContext())
                    .load(config.getPhotoBase64Str())
                    //.apply(mRequestOptions)
                    .centerCrop()
                    .into(imageView);
        }
    }

    public void onClick(View v) {
        int id = v.getId();

    }

    
    /**
     *
     * desc:获取当前副屏是否展示（关闭时双屏同显，显示时双屏异显）
     * @author xia weihao
     * @date 2024/6/20
     * 0 关闭
     * 1 显示
     * -1 暂不支持（可能是单屏设备）
     */ 
    public static int getSecondaryScreenState(Context context) {
        if (isSupportedDifferentDisplay(context)) {
            if (screenPage != null && screenPage.isShowing()) {
                return 1;
            } else {
                return 0;
            }
        }

        return -1;
    }

    /**
     *
     * desc:是否支持双屏显示
     * @author xia weihao
     * @date 2024/6/20
     *
     */
   private static boolean isSupportedDifferentDisplay(Context context) {
        DisplayManager mDisplayManager = (DisplayManager) context.
                getApplicationContext().getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = mDisplayManager.getDisplays();

        if(displays.length > 1) {
            return true;
        }
        return false;
    }

    /**
     *
     * desc:开启副屏
     * @author xia weihao
     * @date 2024/6/20
     *
     */ 
    public static void open(Context context,ConfigBean config) {
        if (screenPage == null) {
            synchronized (SecondaryScreenPage.class) {
                if(screenPage==null) {
                    create(context,config);
                    return;
                }
            }
        }else if(!screenPage.isShowing()){
            screenPage.show();
        }
        screenPage.updateConfig(config);
    }
    
    /**
     *
     * desc:创建并开启副屏
     * @author xia weihao
     * @date 2024/6/20
     *
     */ 
    private static void create(Context context,ConfigBean config){
        DisplayManager mDisplayManager = (DisplayManager) context
                .getApplicationContext()
                .getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = mDisplayManager.getDisplays();
        if (displays.length > 1) {
            Display display = displays[1];
            screenPage = new SecondaryScreenPage(context, display, config);
            screenPage.show();
        }
    }

    /**
     *
     * desc:关闭副屏
     * @author chenjunxu
     * @date 2024/6/20
     *
     */ 
    public static void close(){
        if(screenPage!=null){
            if(screenPage.isShowing()) {
                screenPage.dismiss();
            }
            screenPage.onRelease();
            screenPage=null;
        }

    }
    
    /**
     *
     * @author xwh
     * Time 2024/11/1
     * Description: 释放
     *
    **/
    public void onRelease(){
        imageView = null;
    }

}
