using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using static ET.GameSetting;
using static System.Net.Mime.MediaTypeNames;

namespace ET.Client
{
	[FriendOf(typeof(UISettingComponent))]
	public static class UISettingComponentSystem
    {
		[ObjectSystem]
		public class UISettingComponentAwakeSystem : AwakeSystem<UISettingComponent>
		{
			protected override void Awake(UISettingComponent self, params object[] param)
			{
                ReferenceCollector rc = self.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
                self.verCheckBtn = rc.Get<GameObject>("verCheckBtn");
                self.verCheckBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnVerCheckBtn(); });
                self.quitLoginBtn = rc.Get<GameObject>("quitLoginBtn");
                self.quitLoginBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnQuitLoginBtn(); });
                self.quitGameBtn = rc.Get<GameObject>("quitGameBtn");
                self.quitGameBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnQuitGameBtn(); });
                self.gameoBgVedioBtn = rc.Get<GameObject>("gameoBgVedioBtn");
                self.gameoBgVedioBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnGameoBgVedioBtn(); });
                self.gameoInVedioBtn = rc.Get<GameObject>("gameoInVedioBtn");
                self.gameoInVedioBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnGameoInVedioBtn(); });
                self.closeBtn = rc.Get<GameObject>("closeBtn");
                self.closeBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCloseBtn(); });
                self.gameoBgVedioSlider = rc.Get<GameObject>("gameoBgVedioSlider");
                self.gameoBgVedioSlider.GetComponent<Slider>().onValueChanged.AddListener((val) => { self.OnGameoBgVedioSlider(val); });
                self.gameoInVedioSlider = rc.Get<GameObject>("gameoInVedioSlider");
                self.gameoInVedioSlider.GetComponent<Slider>().onValueChanged.AddListener((val) => { self.OnGameoInVedioSlider(val); });
                self.Init();
            }
        }

        private static async void Init(this UISettingComponent self)
        {
            var value = GameSetting.Instance.GetFloat(MusicSets.Mute_BG);
            self.gameoBgVedioSlider.GetComponent<Slider>().value = value;
            self.isGameBgVedio = value > 0? false : true;
            string abName = self.isGameBgVedio ? "setting_min_sound_sign" : "setting_max_sound_sign";
            await ResourcesComponentHelper.Instance.LoadSprite(self.ClientScene(), "setingatlas");
            Sprite sprite = ResourcesComponentHelper.Instance.GetSprite("setingatlas", abName);
            self.gameoBgVedioBtn.GetComponent<UnityEngine.UI.Image>().sprite = sprite;

            value = GameSetting.Instance.GetFloat(MusicSets.Mute_Music);
            self.gameoInVedioSlider.GetComponent<Slider>().value = value;
            self.isGameInVedio = value > 0 ? false : true;
            abName = self.isGameInVedio ? "setting_min_sound_sign" : "setting_max_sound_sign";
            await ResourcesComponentHelper.Instance.LoadSprite(self.ClientScene(), "setingatlas");
            sprite = ResourcesComponentHelper.Instance.GetSprite("setingatlas", abName);
            self.gameoInVedioBtn.GetComponent<UnityEngine.UI.Image>().sprite = sprite;
        }

        public static async void OnVerCheckBtn(this UISettingComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
        }

        public static async void OnQuitGameBtn(this UISettingComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            UnityEngine.Application.Quit();
        }

        public static async void OnQuitLoginBtn(this UISettingComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            await UIHelper.Remove(self.ClientScene(), UIType.UIMain);
            await UIHelper.Remove(self.ClientScene(), UIType.UISetting);
            await EventSystem.Instance.PublishAsync(GameUtil.Instance.GetSceneComponent(), new EventType.AppStartInitFinish());
        }

        public static async void OnGameoBgVedioBtn(this UISettingComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            self.isGameBgVedio = !self.isGameBgVedio;
            string abName = self.isGameBgVedio ? "setting_min_sound_sign" : "setting_max_sound_sign";
            await ResourcesComponentHelper.Instance.LoadSprite(self.ClientScene(),"setingatlas");
            Sprite sprite = ResourcesComponentHelper.Instance.GetSprite("setingatlas",abName);
            self.gameoBgVedioBtn.GetComponent<UnityEngine.UI.Image>().sprite = sprite;
            SoundManager.Instance.BGMute = self.isGameBgVedio;
            float value = GameSetting.Instance.GetFloat(MusicSets.Mute_BG);
            if (self.isGameBgVedio)
            {
                value = 0;
            }
            else
            {
                value = value == 0? 1 : value;  
            }
            self.gameoBgVedioSlider.GetComponent<Slider>().value = value;
            GameSetting.Instance.SetFloat(MusicSets.Mute_BG, value);
        }

        public static async void OnGameoInVedioBtn(this UISettingComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            self.isGameInVedio = !self.isGameInVedio;
            string abName = self.isGameInVedio ? "setting_min_sound_sign" : "setting_max_sound_sign";
            await ResourcesComponentHelper.Instance.LoadSprite(self.ClientScene(), "setingatlas");
            Sprite sprite = ResourcesComponentHelper.Instance.GetSprite("setingatlas", abName);
            self.gameoInVedioBtn.GetComponent<UnityEngine.UI.Image>().sprite = sprite;
            SoundManager.Instance.MusicMute = self.isGameInVedio;
            float value = GameSetting.Instance.GetFloat(MusicSets.Mute_Music);
            if (self.isGameInVedio)
            {
                value = 0;
            }
            else
            {
                value = value == 0 ? 1 : value;
            }
            self.gameoInVedioSlider.GetComponent<Slider>().value = value;
            GameSetting.Instance.SetFloat(MusicSets.Mute_Music, value);
        }

        public static async void OnCloseBtn(this UISettingComponent self)
        {
            await SoundManager.Instance.PlaySound("clickbtnsound");
            self.OnCloseAsync();
        }

        public static async void OnGameoBgVedioSlider(this UISettingComponent self,float val)
        {
            if (val < 0)
            {
                val = 0;
            }
            self.isGameBgVedio = val == 0?true:false;
            string abName = self.isGameBgVedio ? "setting_min_sound_sign" : "setting_max_sound_sign";
            await ResourcesComponentHelper.Instance.LoadSprite(self.ClientScene(), "setingatlas");
            Sprite sprite = ResourcesComponentHelper.Instance.GetSprite("setingatlas", abName);
            self.gameoBgVedioBtn.GetComponent<UnityEngine.UI.Image>().sprite = sprite;
            SoundManager.Instance.BGMute = self.isGameBgVedio;
            SoundManager.Instance.BGVolume = val;
            GameSetting.Instance.SetFloat(MusicSets.Mute_BG, val);
        }

        public static async void OnGameoInVedioSlider(this UISettingComponent self, float val)
        {
            if (val < 0)
            {
                val = 0;
            }
            self.isGameInVedio = val == 0 ? true : false;
            string abName = self.isGameInVedio ? "setting_min_sound_sign" : "setting_max_sound_sign";
            await ResourcesComponentHelper.Instance.LoadSprite(self.ClientScene(), "setingatlas");
            Sprite sprite = ResourcesComponentHelper.Instance.GetSprite("setingatlas", abName);
            self.gameoInVedioBtn.GetComponent<UnityEngine.UI.Image>().sprite = sprite;
            SoundManager.Instance.MusicMute = self.isGameInVedio;
            SoundManager.Instance.MusicVolume = val;
            GameSetting.Instance.SetFloat(MusicSets.Mute_Music, val);
        }

        public static async void OnCloseAsync(this UISettingComponent self)
        {
            await UIHelper.Remove(self.ClientScene(),UIType.UISetting);
        }
    }
}