class OutlookUserPreferences(ExtraUserPreferences):
    fields = {
        'outlook_active':
        BooleanField(
            _('Sync with Outlook'),
            widget=SwitchWidget(),
            description=_(
                'Add Indico events in which I participate to my Outlook '
                'calendar')),
        'outlook_status':
        SelectField(_('Outlook entry status'),
                    [HiddenUnless('extra_outlook_active', preserve_data=True)],
                    choices=_status_choices,
                    description=_('The status for Outlook Calendar entries'))
    }

    def load(self):
        default_status = OutlookPlugin.settings.get('status')
        return {
            'outlook_active':
            OutlookPlugin.user_settings.get(self.user, 'enabled'),
            'outlook_status':
            OutlookPlugin.user_settings.get(self.user, 'status',
                                            default_status)
        }

    def save(self, data):
        OutlookPlugin.user_settings.set_multi(self.user, {
            'enabled': data['outlook_active'],
            'status': data['outlook_status']
        })
class SettingsForm(IndicoForm):
    debug = BooleanField(
        _('Debug mode'),
        widget=SwitchWidget(),
        description=_(
            "If enabled, requests are not sent to the API but logged instead"))
    service_url = URLField(
        _('Service URL'), [URL(require_tld=False)],
        description=_("The URL of the CERN calendar service"))
    username = StringField(
        _('Username'), [DataRequired()],
        description=_(
            "The username used to authenticate with the CERN calendar service")
    )
    password = IndicoPasswordField(
        _('Password'), [DataRequired()],
        toggle=True,
        description=_(
            "The password used to authenticate with the CERN calendar service")
    )
    status = SelectField(
        _('Status'), [DataRequired()],
        choices=_status_choices,
        description=_("The default status of the event in the calendar"))
    reminder = BooleanField(_('Reminder'),
                            description=_("Enable calendar reminder"))
    reminder_minutes = IntegerField(
        _('Reminder time'), [NumberRange(min=0)],
        description=_("Remind users X minutes before the event"))
    id_prefix = StringField(
        _('Prefix'),
        description=_(
            "Prefix for calendar item IDs. If you change this, existing calendar entries "
            "cannot be deleted/updated anymore!"))
    timeout = FloatField(_('Request timeout'), [NumberRange(min=0.25)],
                         description=_("Request timeout in seconds"))
    max_event_duration = TimeDeltaField(
        _('Maximum Duration'), [DataRequired()],
        units=('days', ),
        description=_('Events lasting longer will not be sent to Exchange'))
from indico.core.plugins import IndicoPlugin
from indico.core.settings.converters import TimedeltaConverter
from indico.modules.events import Event
from indico.modules.events.registration.models.registrations import RegistrationState
from indico.modules.users import ExtraUserPreferences
from indico.web.forms.base import IndicoForm
from indico.web.forms.fields import IndicoPasswordField, TimeDeltaField
from indico.web.forms.validators import HiddenUnless
from indico.web.forms.widgets import SwitchWidget

from indico_outlook import _
from indico_outlook.calendar import update_calendar
from indico_outlook.models.queue import OutlookAction, OutlookQueueEntry
from indico_outlook.util import get_participating_users, is_event_excluded, latest_actions_only

_status_choices = [('free', _('Free')), ('busy', _('Busy')),
                   ('tentative', _('Tentative')), ('oof', _('Out of office'))]


class SettingsForm(IndicoForm):
    debug = BooleanField(
        _('Debug mode'),
        widget=SwitchWidget(),
        description=_(
            "If enabled, requests are not sent to the API but logged instead"))
    service_url = URLField(
        _('Service URL'), [URL(require_tld=False)],
        description=_("The URL of the CERN calendar service"))
    username = StringField(
        _('Username'), [DataRequired()],
        description=_(