using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UIFramework;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using static PerformanceAdjusting;
using System.Linq;
public class ToggleController
{
private SignalEnum _functionId = 0;
private List<Toggle> _toggles;
private List<Text> _texts;
Action<int, List<Text>> _handle;
private int _lastIndex = 0;
private bool _rebound = false;
public ToggleController(SignalEnum funcid, List<Toggle> toggles, List<Text> texts, Action<int, List<Text>> handle)
{
_functionId = funcid;
_toggles = toggles;
_texts = texts;
_handle = handle;
for (int i = 0; i < _toggles.Count; i++)
{
int index = i;
_toggles[i].onValueChanged.AddListener((bool isOn) =>
{
if (isOn && !_rebound)
{
SuperSportNative.SetInt((int)_functionId, PerformanceDataCenter.toggleMapping[_functionId][(ToggleID)index]);
_handle(index, _texts);
ToggleRebound(index, PerformanceDataCenter.toggleMapping[_functionId][(ToggleID)index]);
}
});
}
GetCurrentIndex();
}
private void ToggleRebound(int index, int value)
{
float timeCount = 0;
DOTween.To(() => timeCount, a => timeCount = a, 1, 2).OnComplete(() =>
{
int receive = 20;
if (receive == value)
{
Debug.Log("设置成功");
_lastIndex = index;
}
else
{
Debug.Log("设置失败,按钮回弹");
_rebound = true;
_toggles[_lastIndex].isOn = true;
_handle(_lastIndex, _texts);
_rebound = false;
}
});
}
private void GetCurrentIndex()
{
_rebound = true;
int index = (int)PerformanceDataCenter.toggleMapping[_functionId].FirstOrDefault().Key;
index = 1;
_lastIndex = index;
_toggles[index].isOn = true;
_handle(_lastIndex, _texts);
_rebound = false;
}
}