package com.xunao.effectdemo.dlan;

import android.os.Handler;
import android.text.TextUtils;

import com.xunao.effectdemo.App;
import com.hpplay.sdk.source.api.LelinkPlayerInfo;
import com.hpplay.sdk.source.api.LelinkSourceSDK;
import com.hpplay.sdk.source.bean.BrowserConfigBean;
import com.hpplay.sdk.source.bean.MediaAssetBean;
import com.hpplay.sdk.source.browse.api.LelinkServiceInfo;

import java.util.concurrent.ExecutorService;

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/9/1916:56
 * desc :
 * version: 1.0
 */
public class DLNAControlCenter {

	private volatile static DLNAControlCenter dlnaCenter;
	private ExecutorService executorService;
	private long startMiracastTime;
	private Handler handler;
	private boolean isDlnaMode = false;
	//dlna投屏相关
	private final static int PUSH_SEEK = 3; // 投屏seek
	private final static int PUSH_STATE_LISTENING = 4;//投屏状态监听
	private final static int PUSH_STATE_BACK = 5;//投屏状态返回
	private final static int PUSH_PLAY = 6; // 投屏开始

	public static DLNAControlCenter getInstance() {
		if (null == dlnaCenter) {
			synchronized (DLNAControlCenter.class) {
				if (dlnaCenter == null) {
					dlnaCenter = new DLNAControlCenter();
				}
			}
		}
		return dlnaCenter;
	}


	private DLNAControlCenter() {
//        executorService = Executors.newCachedThreadPool();
		DeviceManager connectManager = DeviceManager.getInstance();
		//sdk初始化
		LelinkSourceSDK.getInstance()
				.setBindSdkListener(connectManager.getBindListener())
				.setBrowseResultListener(connectManager.getBrowseListener())
				.setConnectListener(connectManager.getConnectListener())
				.setPlayListener(CastManager.getInstance().getLelinkPlayerListener())
				//FIXME WARN: 这里替换为您申请的AppID & AppSecret，build.gradle替换为您的应用包名
//                .setSdkInitInfo(AppVarManager.getInstance().getBaseContext(), "17894", "69ce8955094258d339c1b6eadef2ec09")
				.setSdkInitInfo(App.getInstance(), "17894", "69ce8955094258d339c1b6eadef2ec09")
				.bindSdk();
	}

	public synchronized void startLelinkSearch() {
		try {
			BrowserConfigBean configBean = new BrowserConfigBean();
			configBean.useLelink = true;
			configBean.useDlna = true;
			configBean.useBLE = true;
			configBean.useSonic = true;
			LelinkSourceSDK.getInstance().startBrowse(configBean);
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}

	}

	/**
	 * dlna播放
	 *
	 * @param device
	 * @param castUrl
	 */
	public synchronized void dlnaPlay(LelinkServiceInfo device, String castUrl, String titleName, double duration) {
		if (device == null || TextUtils.isEmpty(castUrl)) {
			return;
		}
		try {
			LelinkSourceSDK.getInstance().stopPlay();
			startMiracastTime = System.currentTimeMillis();
			LelinkPlayerInfo lelinkPlayerInfo = new LelinkPlayerInfo();
			lelinkPlayerInfo.setUrl(castUrl);
			lelinkPlayerInfo.setType(LelinkSourceSDK.MEDIA_TYPE_VIDEO);
			MediaAssetBean mediaAssetBean = new MediaAssetBean();
			mediaAssetBean.setName(titleName);
			// 非必要参数，部分dlna不返回视频总长度，实现下一集时若需要兼容这种dlna接收端，需设置媒资总长度，单位秒
			mediaAssetBean.setDuration((long) duration);
			lelinkPlayerInfo.setMediaAsset(mediaAssetBean);
			lelinkPlayerInfo.setLelinkServiceInfo(device);
			LelinkSourceSDK.getInstance().startPlayMedia(lelinkPlayerInfo);
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}

	}

	/**
	 * 投屏跳转相应位置
	 *
	 * @param device
	 */
	public synchronized void dlnaSeekToPlayPosition(LelinkServiceInfo device, int progress) {
		try {
			LelinkSourceSDK.getInstance().seekTo(progress);
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}

	}

	/**
	 * dlna暂停
	 *
	 * @param curTPDevice
	 */
	public synchronized void dlnaPause(LelinkServiceInfo curTPDevice) {
		if (curTPDevice == null) return;
		try {
			LelinkSourceSDK.getInstance().pause();
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}

	}

	/**
	 * dlna开始
	 */
	public synchronized void dlnaResume() {
		try {
			LelinkSourceSDK.getInstance().resume();
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}

	}

	/**
	 * dlna投屏停止
	 *
	 * @param curTPDevice
	 */
	public synchronized void dlnaStop(LelinkServiceInfo curTPDevice, String titleId) {
		try {

			LelinkSourceSDK.getInstance().stopPlay();
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}

//        dlnaCenter = null;
	}

	/**
	 * dlna设置音量
	 */
	public synchronized void dlnaVolume(boolean isAddVolume) {
		try {
			if (isAddVolume) {
				LelinkSourceSDK.getInstance().addVolume();
			} else {
				LelinkSourceSDK.getInstance().subVolume();
			}
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}

	/**
	 * dlan上一次暂停位置上播放
	 *
	 * @param curTPDevice
	 */
	public synchronized void dlnaGoonPlayPosition(LelinkServiceInfo curTPDevice, int progress) {
		if (curTPDevice == null) return;
		try {
			LelinkSourceSDK.getInstance().seekTo(progress);
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}


	}

	public void setUIListener(IUIUpdateListener mUIListener) {
		DeviceManager.getInstance().setUIListener(mUIListener);
	}

}
