﻿using Skype4COMWrapper;
using Skypelin.Modules;
using Skypelin.Modules.WakeUp;

namespace Skypelin.ViewModels.WakeUpModule
{
    internal class WakeUpMainVm
    {
        private readonly Skype _skype;

        private ISkypeMessageHandler _wakeUpHandler;

        public WakeUpMainVm(Skype skype)
        {
            _skype = skype;

            triggerWakeUpModule(WakeUpModuleIsEnabled);
        }

        public bool WakeUpModuleIsEnabled
        {
            get { return WakeUpSettings.Instance.Load<bool>(); }

            set
            {
                WakeUpSettings.Instance.Save(value);
                triggerWakeUpModule(value);
            }
        }

        private void triggerWakeUpModule(bool isEnabled)
        {
            // If enabled then subscribe to events. Otherwise, unsubscribe.
            // If app starts with disabled module then don't even create handler.
            if (isEnabled)
            {
                if (_wakeUpHandler == null)
                    _wakeUpHandler = new WakeUpHandler(_skype);

                _skype.MessageStatus += _wakeUpHandler.Handle;
            }
            else if (_wakeUpHandler != null)
            {
                _skype.MessageStatus -= _wakeUpHandler.Handle;
            }
        }
    }
}
