using AdaptUI;
using AppUI.Bluetooth;
using AppUI.Manager.View;
using AppUI.View.Component;
using SmartBowSDK;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using BLEAimhandler = AppUI.Bluetooth.AimHandler;
using SdkAimDeviceInfo = SmartBowSDK.AimDeviceInfo;
namespace AppUI.View.Home.DeviceView
{
/// content-has 下各玩家 ItemShow;显隐由 AimDeviceInfo 是否已记录决定。
[Serializable]
public class DeviceViewPlayerSlot
{
public BluetoothPlayer player = BluetoothPlayer.FIRST_PLAYER;
public DeviceView_ItemShow itemShow;
}
public class DeviceView : MonoBehaviour, MenuBackInterface
{
[Header("连接态 UI(未连接 content-not / 已连接 content-has)")]
[SerializeField] GameObject ConnectNotParent;
[SerializeField] GameObject ConnectHasParent;
[Header("1P 设备信息(已连接态下刷新)")]
[SerializeField] DeviceView_ItemShow itemShowP1;
[Header("各玩家连接态 ItemShow(连接成功后显示对应子物体)")]
[SerializeField] List playerSlots = new List();
[SerializeField] List smartConnect1Buttons;
[SerializeField] List smartArcheryBg;
[SerializeField] List deviceViewItems;
SmartBowDeviceHub _hub;
bool _lastP1HasRecord;
bool _lastP2HasRecord;
bool _hubEventsBound;
bool _deviceSelectionFlowActive;
/// 当前激活的 DeviceView(用于子页面结束临时选型流程)。
public static DeviceView Active { get; private set; }
public bool IsDeviceSelectionFlowActive => _deviceSelectionFlowActive;
void Awake()
{
EnsurePlayerSlots();
}
public bool OnMenuBack()
{
ViewManager.HideView(UIViewType.DeviceView);
return true;
}
public void OnClick_Back()
{
AudioMgr.ins.PlayBtn();
if (_deviceSelectionFlowActive) {
EndDeviceSelectionFlow();
}
else {
ViewManager.HideView(UIViewType.DeviceView);
}
}
void OnEnable()
{
Active = this;
EnterManualConnectFlow();
TryBindHubEvents();
RefreshConnectContent(force: true);
}
void OnDisable()
{
ExitManualConnectFlow();
EndDeviceSelectionFlow();
if (Active == this)
Active = null;
UnbindHubEvents();
}
void Start()
{
PersistenHandler.ins?.menuBackCtr.views.Add(this);
for (int i = 0; i < smartConnect1Buttons.Count; i++)
{
int temp = i;
smartConnect1Buttons[i].onClick.AddListener(() =>
{
OnChangeSmartConnect1Button(temp);
});
}
SmartBowDeviceHub.ins?.Aim?.onCreateAimDeviceInfoById();
TryBindHubEvents();
RefreshConnectContent(force: true);
}
void OnDestroy()
{
UnbindHubEvents();
PersistenHandler.ins?.menuBackCtr.views.Remove(this);
SmartBowDeviceHub.ins?.CancelConnecting();
}
void Update()
{
if (!_hubEventsBound)
TryBindHubEvents();
}
void TryBindHubEvents()
{
SmartBowDeviceHub hub = SmartBowDeviceHub.ins;
if (hub == null || _hubEventsBound) return;
_hub = hub;
_hub.OnStateChanged += OnHubStateChanged;
if (_hub.Aim != null)
_hub.Aim.aimDeviceInfoChangeEvent += OnDeviceInfoChanged;
_hubEventsBound = true;
}
void UnbindHubEvents()
{
if (!_hubEventsBound || _hub == null) return;
_hub.OnStateChanged -= OnHubStateChanged;
if (_hub.Aim != null)
_hub.Aim.aimDeviceInfoChangeEvent -= OnDeviceInfoChanged;
_hub = null;
_hubEventsBound = false;
}
void OnHubStateChanged(SmartBowDeviceState state)
{
// 连接态文案仍随 SDK 状态刷新;ItemShow 显隐只看 AimDeviceInfo 是否已记录。
RefreshPlayerItemShows(updateVisibility: false);
}
void OnDeviceInfoChanged()
{
if (!isActiveAndEnabled) return;
RefreshConnectContent(force: true);
}
///
/// 是否已在本地记录过该玩家的设备信息(选过类型且连通过写入 MAC)。
/// 与 / type 一致,不表示当前在线。
///
static bool HasRecordedDeviceInfo(BLEAimhandler aim, BluetoothPlayer player)
{
if (aim == null) return false;
SdkAimDeviceInfo info = aim.GetAimDeviceInfo(player);
if (info == null) return false;
return info.bInitMac && aim.GetDeviceType(player) != -1;
}
void EnsurePlayerSlots()
{
if (playerSlots != null && playerSlots.Count > 0)
return;
playerSlots = new List();
if (deviceViewItems != null)
{
foreach (DeviceView_ItemShow item in deviceViewItems)
{
if (item == null) continue;
playerSlots.Add(new DeviceViewPlayerSlot
{
player = item.Player,
itemShow = item
});
}
}
if (itemShowP1 != null && !ContainsSlot(playerSlots, itemShowP1))
{
playerSlots.Insert(0, new DeviceViewPlayerSlot
{
player = BluetoothPlayer.FIRST_PLAYER,
itemShow = itemShowP1
});
}
}
static bool ContainsSlot(List slots, DeviceView_ItemShow item)
{
for (int i = 0; i < slots.Count; i++)
{
if (slots[i].itemShow == item)
return true;
}
return false;
}
/// 切换设备时临时回到 content-not,走弓箭/枪选型入口。
public void BeginDeviceSelectionFlow()
{
EnterManualConnectFlow();
_deviceSelectionFlowActive = true;
BLEAimhandler aim = SmartBowDeviceHub.ins?.Aim;
bool p1HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.FIRST_PLAYER);
bool p2HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.SECOND_PLAYER);
ApplyConnectParentVisibility(p1HasRecord, p2HasRecord);
}
/// 结束临时选型流程,恢复 content-not / content-has 正常显隐。
public void EndDeviceSelectionFlow()
{
if (!_deviceSelectionFlowActive)
return;
_deviceSelectionFlowActive = false;
RefreshConnectContent(force: true);
}
/// 按 AimDeviceInfo 是否已记录设备信息切换 content-not / content-has,并显示对应 ItemShow。
public void RefreshConnectContent(bool force = false)
{
BLEAimhandler aim = SmartBowDeviceHub.ins?.Aim;
bool p1HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.FIRST_PLAYER);
bool p2HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.SECOND_PLAYER);
if (!force && !_deviceSelectionFlowActive
&& p1HasRecord == _lastP1HasRecord && p2HasRecord == _lastP2HasRecord)
return;
_lastP1HasRecord = p1HasRecord;
_lastP2HasRecord = p2HasRecord;
ApplyConnectParentVisibility(p1HasRecord, p2HasRecord);
RefreshPlayerItemShows(updateVisibility: true);
}
void ApplyConnectParentVisibility(bool p1HasRecord, bool p2HasRecord)
{
if (_deviceSelectionFlowActive)
{
if (ConnectNotParent != null)
ConnectNotParent.SetActive(true);
if (ConnectHasParent != null)
ConnectHasParent.SetActive(false);
return;
}
bool anyHasRecord = p1HasRecord || p2HasRecord;
if (ConnectNotParent != null)
ConnectNotParent.SetActive(!anyHasRecord);
if (ConnectHasParent != null)
ConnectHasParent.SetActive(anyHasRecord);
}
/// 按 AimDeviceInfo 激活 ItemShow;updateVisibility 为 false 时只刷新已显示项的状态文案。
public void RefreshPlayerItemShows(bool updateVisibility = true)
{
EnsurePlayerSlots();
BLEAimhandler aim = SmartBowDeviceHub.ins?.Aim;
if (playerSlots == null) return;
foreach (DeviceViewPlayerSlot slot in playerSlots)
{
if (slot.itemShow == null) continue;
bool show = HasRecordedDeviceInfo(aim, slot.player);
if (updateVisibility)
{
if (slot.itemShow.gameObject.activeSelf != show)
slot.itemShow.gameObject.SetActive(show);
}
else if (!slot.itemShow.gameObject.activeSelf)
{
continue;
}
if (!show) continue;
slot.itemShow.onShowDeviceInfo();
slot.itemShow.onUpdateStatusInfo();
slot.itemShow.RefreshActionButtons();
}
}
public void RefreshItemShowP1()
{
RefreshPlayerItemShows();
}
DeviceView_ItemShow GetItemShowP1()
{
if (itemShowP1 != null) return itemShowP1;
if (playerSlots != null)
{
foreach (DeviceViewPlayerSlot slot in playerSlots)
{
if (slot.player == BluetoothPlayer.FIRST_PLAYER && slot.itemShow != null)
return slot.itemShow;
}
}
if (deviceViewItems != null && deviceViewItems.Count > 0)
return deviceViewItems[0];
return null;
}
public void OnChangeSmartConnect1Button(int index)
{
bool _selected = false;
for (int i = 0; i < smartConnect1Buttons.Count; i++)
{
Button _button = smartConnect1Buttons[i];
Color32 _white;
if (index == i)
{
_white = new Color32(255, 255, 255, 255);
_selected = true;
_button.GetComponent().sprite = smartArcheryBg[1];
}
else
{
_white = new Color32(59, 59, 59, 255);
_button.GetComponent().sprite = smartArcheryBg[0];
}
_button.transform.Find("icon").GetComponent().color = _white;
_button.transform.Find("title").GetComponent().color = _white;
}
if (_selected)
{
AudioMgr.ins.PlayBtn();
if (getEnabelPanelStatus())
{
EnterManualConnectFlow();
if (index == 0)
{
Debug.Log("archery");
ViewManager.ShowView(UIViewType.DeviceArcheryView);
}
else if (index == 1)
{
Debug.Log("Gun");
ViewManager.ShowView(UIViewType.DeviceGunView);
}
SmartBowDeviceHub.ins.Aim.onCreateTempAimDeviceInfo();
}
}
}
public bool getEnabelPanelStatus() => true;
/// 进入 DeviceView 手动连接:抑制 Home 自动连接,并取消进行中的自动连。
void EnterManualConnectFlow()
{
SmartBowDeviceHub hub = SmartBowDeviceHub.ins;
if (hub == null) return;
hub.SetSuppressHomeAutoConnect(true);
if (hub.IsConnecting(BluetoothPlayer.FIRST_PLAYER) || hub.IsConnecting(BluetoothPlayer.SECOND_PLAYER))
hub.CancelConnecting(global::BluetoothStatusEnum.Connect);
}
void ExitManualConnectFlow()
{
SmartBowDeviceHub.ins?.SetSuppressHomeAutoConnect(false);
}
}
}