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

public class PushSettingsDialog : MonoBehaviour
{
    public Action<bool?, bool?> action;

    public GameObject panel;

    public Toggle checkDay;
    public Toggle checkNight;

    public Slider enabledDay;
    public Slider enabledNight;

    public bool? enableDay;
    public bool? enableNight;

    public static void Show(Action<bool?, bool?> action)
    {
        Present(action);
    }

    public static void Present(Action<bool?, bool?> action)
    {
        GameObject pushSettingsDialogPanel = GameObject.Find("PushSettingsDialogPanel");

        Image image = pushSettingsDialogPanel.GetComponent<Image>();
        image.enabled = true;

        PushSettingsDialog pushSettingsDialog = pushSettingsDialogPanel.GetComponent<PushSettingsDialog>();
        pushSettingsDialog.panel.SetActive(true);
        pushSettingsDialog.action = action;
    }

    public static void Dismiss()
    {
        GameObject pushSettingsDialogPanel = GameObject.Find("PushSettingsDialogPanel");

        Image image = pushSettingsDialogPanel.GetComponent<Image>();
        image.enabled = false;

        PushSettingsDialog pushSettingsDialog = pushSettingsDialogPanel.GetComponent<PushSettingsDialog>();
        pushSettingsDialog.panel.SetActive(false);
    }

    public void OnClick(int tag)
    {
        if (tag == 0)
        {
            action.Invoke(enableDay, enableNight);
        }
        else
        {
            action.Invoke(null, null);
        }
        
        Dismiss();
    }

    public void OnEnabledDay()
    {
        enableDay = ((int)enabledDay.value == 1) ? true:false;
    }

    public void OnEnabledNight()
    {
        enableNight = ((int)enabledNight.value == 1) ? true : false;
    }

    public void OnCheckDay()
    {
        enabledDay.interactable = checkDay.isOn;

        if (!checkDay.isOn)
        {
            enableDay = null;
        }
        else
        {
            OnEnabledDay();
        }
    }

    public void OnCheckNight()
    {
        enabledNight.interactable = checkNight.isOn;

        if (!checkNight.isOn)
        {
            enableNight = null;
        }
        else
        {
            OnEnabledNight();
        }
    }
}
