package com.xunao.effectdemo.dlan;

import android.text.TextUtils;
import android.util.Log;

import com.hpplay.sdk.source.api.IBindSdkListener;
import com.hpplay.sdk.source.api.IConnectListener;
import com.hpplay.sdk.source.api.IRelevantInfoListener;
import com.hpplay.sdk.source.api.LelinkSourceSDK;
import com.hpplay.sdk.source.browse.api.IBrowseListener;
import com.hpplay.sdk.source.browse.api.LelinkServiceInfo;

import java.util.List;

/**
 * 连接
 */
public class DeviceManager {
    private final static String TAG = "DemoDeviceManager";

    private static DeviceManager sInstance = null;
    private LelinkServiceInfo mSelectInfo;
    private IUIUpdateListener mUIListener;

    private IBindSdkListener mBindSdkListener = new IBindSdkListener() {
        @Override
        public void onBindCallback(boolean success) {
            Log.i(TAG, "onBindCallback " + success);
            setPassThroughListener();
            if (success) {
                Log.e("LeBo","初始化:"+success);
                if (mUIListener != null) {
                    mUIListener.onBindSuccess();
                }

            }
        }
    };

    private IBrowseListener mBrowseListener = new IBrowseListener() {

        @Override
        public void onBrowse(int i, List<LelinkServiceInfo> list) {
            if (i == IBrowseListener.BROWSE_ERROR_AUTH) {
                Log.e(TAG, "授权失败");
                return;
            }
            if (i == IBrowseListener.BROWSE_STOP) {
                Log.i(TAG, "搜索停止");
            } else if (i == IBrowseListener.BROWSE_TIMEOUT) {
                Log.i(TAG, "搜索超时");
            }
            if (mUIListener != null) {
                mUIListener.onUpdateDevices(list);
            }
        }
    };

    private IConnectListener mConnectListener = new IConnectListener() {
        @Override
        public void onConnect(LelinkServiceInfo lelinkServiceInfo, int protocol) {
            Log.i(TAG, "onConnect:" + lelinkServiceInfo.getName());
            String type = protocol == IConnectListener.TYPE_LELINK ? "Lelink"
                    : protocol == IConnectListener.TYPE_DLNA ? "DLNA"
                    : protocol == IConnectListener.TYPE_IM ? "IM" : ("协议:" + protocol);
            setSelectInfo(lelinkServiceInfo);
            if (mUIListener != null) {
                mUIListener.onConnect(lelinkServiceInfo);
            }
        }

        @Override
        public void onDisconnect(LelinkServiceInfo lelinkServiceInfo, int what, int extra) {
            if (lelinkServiceInfo == null) {
                return;
            }
            Log.i(TAG, "onDisconnect:" + lelinkServiceInfo.getName() + " disConnectType:" + what + " extra:" + extra);
            String text = null;
            if (what == IConnectListener.WHAT_HARASS_WAITING) {// 防骚扰，等待用户确认
                // 乐播投屏防骚扰等待消息，请开发者务必处理该消息
                text = lelinkServiceInfo.getName() + "等待用户确认";
            } else if (what == IConnectListener.WHAT_DISCONNECT) {
                switch (extra) {
                    case IConnectListener.EXTRA_HARASS_REJECT:// 防骚扰，用户拒绝投屏
                        text = lelinkServiceInfo.getName() + "连接被拒绝";
                        break;
                    case IConnectListener.EXTRA_HARASS_TIMEOUT:// 防骚扰，用户响应超时
                        text = lelinkServiceInfo.getName() + "防骚扰响应超时";
                        break;
                    case IConnectListener.EXTRA_HARASS_BLACKLIST:// 防骚扰，该用户被加入黑名单
                        text = lelinkServiceInfo.getName() + "已被加入投屏黑名单";
                        break;
                    case IConnectListener.EXTRA_CONNECT_DEVICE_OFFLINE:
                        text = lelinkServiceInfo.getName() + "不在线";
                        break;
                    default:
                        text = lelinkServiceInfo.getName() + "连接断开";
                        break;
                }
            } else if (what == IConnectListener.WHAT_CONNECT_FAILED) {
                switch (extra) {
                    case IConnectListener.EXTRA_CONNECT_DEVICE_OFFLINE:
                        text = lelinkServiceInfo.getName() + "不在线";
                        break;
                    default:
                        text = lelinkServiceInfo.getName() + "连接失败";
                        break;
                }
            }
            if (TextUtils.isEmpty(text)) {
                text = "onDisconnect " + what + "/" + extra;
            }
        }
    };

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

    private DeviceManager() {

    }

    public void setUIListener(IUIUpdateListener listener) {
        this.mUIListener = listener;
    }

    public void setSelectInfo(LelinkServiceInfo serviceInfo) {
        mSelectInfo = serviceInfo;
    }



    public LelinkServiceInfo getSelectInfo() {
        return mSelectInfo;
    }

    public IBindSdkListener getBindListener() {
        return mBindSdkListener;
    }

    public IBrowseListener getBrowseListener() {
        return mBrowseListener;
    }

    public IConnectListener getConnectListener() {
        return mConnectListener;
    }

    /**
     * 设置透传监听
     */
    private void setPassThroughListener() {
        LelinkSourceSDK.getInstance().setPassThroughListener(new IRelevantInfoListener() {
            @Override
            public void onSendRelevantInfoResult(int option, String result) {

            }

            @Override
            public void onReverseInfoResult(int option, final String result) {
                Log.i(TAG, "onReverseInfoResult option = " + option + ", result = " + result);
            }
        });
    }

}
