using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class settingPageHandler : MonoBehaviour, IEventHandler
{
    // Start is called before the first frame update
    void Start()
    {
        utilsTools.onEvent(this, "showSettingView");
        utilsTools.onEvent(this, "changeSelectGameTypeIdx");
        var music = GetSwitchView("group1/music/switch");
        if(music != null)
        {
            music.isOff = getDBBoolDataByKey("music");
            music.onEmptyBack = (GameObject obj) =>
            {
                setDBBoolDataByKey("music", music.isOff);
            };
        }
        var Vibration = GetSwitchView("group1/Vibration/switch");
        if (Vibration != null)
        {
            Vibration.isOff = getDBBoolDataByKey("vibration");
            Vibration.onEmptyBack = (GameObject obj) =>
            {
                setDBBoolDataByKey("vibration", Vibration.isOff);
            };
        }
        var colorPictures = GetSwitchView("group1/ShowColoredPictures/switch");
        if (colorPictures != null)
        {
            colorPictures.isOff = getDBBoolDataByKey("colorPictures");
            colorPictures.onEmptyBack = (GameObject obj) =>
            {
                setDBBoolDataByKey("colorPictures", colorPictures.isOff);
            };
        }
        var autoswitchcolor = GetSwitchView("group2/auto_switch_color/switch");
        if (autoswitchcolor != null)
        {
            autoswitchcolor.isOff = getDBBoolDataByKey("autoswitchcolor");
            autoswitchcolor.onEmptyBack = (GameObject obj) =>
            {
                setDBBoolDataByKey("autoswitchcolor", autoswitchcolor.isOff);
            };
        }
        var color_palette_progress = GetSwitchView("group2/color_palette_progress/switch");
        if (color_palette_progress != null)
        {
            color_palette_progress.isOff = getDBBoolDataByKey("color_palette_progress");
            color_palette_progress.onEmptyBack = (GameObject obj) =>
            {
                setDBBoolDataByKey("color_palette_progress", color_palette_progress.isOff);
            };
        }
        utilsTools.HideView(gameObject);
    }
    void updateUI()
    {
        var music = GetSwitchView("group1/music/switch");
        if (music != null)
        {
            music.isOff = getDBBoolDataByKey("music");
        }
        var Vibration = GetSwitchView("group1/Vibration/switch");
        if (Vibration != null)
        {
            Vibration.isOff = getDBBoolDataByKey("vibration");
        }
        var colorPictures = GetSwitchView("group1/ShowColoredPictures/switch");
        if (colorPictures != null)
        {
            colorPictures.isOff = getDBBoolDataByKey("colorPictures");
        }
        var autoswitchcolor = GetSwitchView("group2/auto_switch_color/switch");
        if (autoswitchcolor != null)
        {
            autoswitchcolor.isOff = getDBBoolDataByKey("autoswitchcolor");
        }
        var color_palette_progress = GetSwitchView("group2/color_palette_progress/switch");
        if (color_palette_progress != null)
        {
            color_palette_progress.isOff = getDBBoolDataByKey("color_palette_progress");
        }
        onUpdateGameSelectAreaType();
    }
    private switchView GetSwitchView(string path)
    {
        var child = transform.Find(path);
        if (child == null) return null;
        return child.GetComponent<switchView>();
    }
    private int getDBIntDataByKey(string key,int defvalue)
    {
        return utilsDB.getDBIntDataByKey(key, defvalue);
    }
    private bool getDBBoolDataByKey(string key)
    {
        return utilsDB.getDBBoolDataByKey(key, false);
    }
    private string getDBStringDataByKey(string key,string defvalue = "")
    {
        return utilsDB.getDBStringDataByKey(key, defvalue);
    }
    private void setDBIntDataByKey(string key, int value)
    {
        utilsDB.setDBIntDataByKey(key, value);
    }
    private void setDBBoolDataByKey(string key,bool value)
    {
        utilsDB.setDBBoolDataByKey(key, value);
    }
    private void setDBStringDataByKey(string key, string value)
    {
        utilsDB.setDBStringDataByKey(key, value);
    }
    // Update is called once per frame
    void Update()
    {
    }
    public void onPrivacyPolicyHandler()
    {
        string url = this.getDBStringDataByKey("privacyPolicy", "http://www.baidu.com");
        utilsTools.showWebView(url);
    }
    public void onTermsOfServiceHandler()
    {
        string url = this.getDBStringDataByKey("termsOfService", "http://www.baidu.com");
        utilsTools.showWebView(url);
    }
    public void onFeedbackHandler()
    {
        utilsTools.sendEvent("onFeedBackPageShow");
    }
    public void onRateUsHandler()
    {
        //评价 调用底层
    }
    public void onPurchaseHandler()
    {
        //内购 暂时未定
    }
    public void onHighlightAreasHandler()
    {
        utilsTools.sendEvent("show_SettingSelectArea");
        //游戏时候的底图
    }
    public void onBackHandler()
    {
        utilsTools.HideView(gameObject);
    }
    public void onUpdateGameSelectAreaType()
    {
        int idx = utilsDB.getDBIntDataByKey("seting_areaSelectIdx", 0);
        var obj = transform.Find("group2/highlightareas/nowUse").gameObject;
        Image image = obj.GetComponent<Image>();
        Debug.Log("fdsfdsfds   " + idx.ToString());
        if(idx == 0)
        {
            image.color = Color.blue;
        }else if(idx == 1)
        {
            image.color = Color.green;
        }else if(idx == 2)
        {
            image.color = Color.red;
        }
        else
        {
            image.color = Color.yellow;
        }
    }
    void IEventHandler.onEventHandler(string eventName, List<Object> uobjects, List<object> objects)
    {
        if (eventName.Equals("showSettingView"))
        {
            utilsTools.ShowView(gameObject);
            this.updateUI();
        }else if (eventName.Equals("changeSelectGameTypeIdx"))
        {
            onUpdateGameSelectAreaType();
        }
    }

}
