import { FormlyFieldConfig } from '@ngx-formly/core';
import { T } from '../../../t.const';
import { RepeatQuickSetting, TaskRepeatCfg } from '../task-repeat-cfg.model';
import { getQuickSettingUpdates } from './get-quick-setting-updates';

const updateParent = (
  field: FormlyFieldConfig,
  changes: Partial<TaskRepeatCfg>,
): void => {
  // possibly better?
  field.form?.patchValue({
    // ...field.parent.model,
    ...changes,
  } as any);
};

export const TASK_REPEAT_CFG_ESSENTIAL_FORM_CFG: FormlyFieldConfig[] = [
  {
    key: 'title',
    type: 'input',
    templateOptions: {
      label: T.F.TASK_REPEAT.F.TITLE,
    },
  },

  {
    key: 'quickSetting',
    type: 'select',
    defaultValue: 'DAILY',
    templateOptions: {
      required: true,
      label: T.F.TASK_REPEAT.F.QUICK_SETTING,
      // NOTE replaced in component to allow for dynamic translation
      options: [],
      change: (field, event) => {
        const updatesForQuickSetting = getQuickSettingUpdates(
          event.value as RepeatQuickSetting,
        );
        if (updatesForQuickSetting) {
          // NOTE: for some reason this doesn't update the model value, just the view value :(
          updateParent(field, updatesForQuickSetting);
        }
      },
    },
  },

  // REPEAT CUSTOM CFG - Wrapped in container
  {
    fieldGroupClassName: 'repeat-config-container',
    resetOnHide: false,
    hideExpression: (model: any) => model.quickSetting !== 'CUSTOM',
    fieldGroup: [
      {
        fieldGroupClassName: 'repeat-cycle',
        fieldGroup: [
          {
            key: 'repeatEvery',
            type: 'input',
            defaultValue: 1,
            templateOptions: {
              label: T.F.TASK_REPEAT.F.REPEAT_EVERY,
              required: true,
              min: 1,
              max: 1000,
              type: 'number',
            },
          },
          {
            key: 'repeatCycle',
            type: 'select',
            defaultValue: 'WEEKLY',
            templateOptions: {
              required: true,
              label: T.F.TASK_REPEAT.F.REPEAT_CYCLE,
              options: [
                { value: 'DAILY', label: T.F.TASK_REPEAT.F.C_DAY },
                { value: 'WEEKLY', label: T.F.TASK_REPEAT.F.C_WEEK },
                { value: 'MONTHLY', label: T.F.TASK_REPEAT.F.C_MONTH },
                { value: 'YEARLY', label: T.F.TASK_REPEAT.F.C_YEAR },
              ],
            },
          },
        ],
      },
      {
        fieldGroupClassName: 'monthly-anchor',
        resetOnHide: false,
        hideExpression: (model: any) => model.repeatCycle !== 'MONTHLY',
        fieldGroup: [
          {
            key: 'monthlyWeekOfMonth',
            type: 'select',
            // Picking the "Day of month" sentinel clears the anchor; the
            // gatekeeper falls back to legacy day-of-month behavior.
            defaultValue: null,
            templateOptions: {
              label: T.F.TASK_REPEAT.F.WEEK_OF_MONTH,
              description: T.F.TASK_REPEAT.F.MONTHLY_MODE_DAY_OF_MONTH_DESCRIPTION,
              options: [
                { value: null, label: T.F.TASK_REPEAT.F.MONTHLY_MODE_DAY_OF_MONTH },
                { value: 1, label: T.F.TASK_REPEAT.F.ORD_FIRST },
                { value: 2, label: T.F.TASK_REPEAT.F.ORD_SECOND },
                { value: 3, label: T.F.TASK_REPEAT.F.ORD_THIRD },
                { value: 4, label: T.F.TASK_REPEAT.F.ORD_FOURTH },
                { value: -1, label: T.F.TASK_REPEAT.F.ORD_LAST },
              ],
            },
          },
          {
            key: 'monthlyWeekday',
            type: 'select',
            defaultValue: 1,
            resetOnHide: false,
            hideExpression: (model: any) => model.monthlyWeekOfMonth == null,
            templateOptions: {
              label: T.F.TASK_REPEAT.F.WEEKDAY,
              options: [
                { value: 1, label: T.F.TASK_REPEAT.F.MONDAY },
                { value: 2, label: T.F.TASK_REPEAT.F.TUESDAY },
                { value: 3, label: T.F.TASK_REPEAT.F.WEDNESDAY },
                { value: 4, label: T.F.TASK_REPEAT.F.THURSDAY },
                { value: 5, label: T.F.TASK_REPEAT.F.FRIDAY },
                { value: 6, label: T.F.TASK_REPEAT.F.SATURDAY },
                { value: 0, label: T.F.TASK_REPEAT.F.SUNDAY },
              ],
            },
          },
        ],
      },
      {
        // Hide via a dynamic CSS class instead of `hideExpression`. With formly's
        // default `lazyRender`, hiding a field group destroys its child views and
        // recreates them on re-show, and the recreated mat-checkboxes lose their
        // wiring to the (re-registered) FormControls. After a cycle round-trip
        // (Week -> Month -> Week) the checkboxes then look enabled but are inert:
        // clicks no longer update the model (#8025). Keeping the group mounted and
        // toggling only its visibility preserves the control/view binding.
        // `resetOnHide: false` on each checkbox keeps the selection when the
        // CUSTOM container itself is hidden (quickSetting != CUSTOM).
        fieldGroupClassName: 'weekdays',
        expressionProperties: {
          className: (model: TaskRepeatCfg) =>
            model.repeatCycle === 'WEEKLY' ? '' : 'repeat-cfg-hidden',
        },
        fieldGroup: [
          {
            key: 'monday',
            type: 'checkbox',
            resetOnHide: false,
            templateOptions: {
              label: T.F.TASK_REPEAT.F.MONDAY,
            },
          },
          {
            key: 'tuesday',
            type: 'checkbox',
            resetOnHide: false,
            templateOptions: {
              label: T.F.TASK_REPEAT.F.TUESDAY,
            },
          },
          {
            key: 'wednesday',
            type: 'checkbox',
            resetOnHide: false,
            templateOptions: {
              label: T.F.TASK_REPEAT.F.WEDNESDAY,
            },
          },
          {
            key: 'thursday',
            type: 'checkbox',
            resetOnHide: false,
            templateOptions: {
              label: T.F.TASK_REPEAT.F.THURSDAY,
            },
          },
          {
            key: 'friday',
            type: 'checkbox',
            resetOnHide: false,
            templateOptions: {
              label: T.F.TASK_REPEAT.F.FRIDAY,
            },
          },
          {
            key: 'saturday',
            type: 'checkbox',
            resetOnHide: false,
            templateOptions: {
              label: T.F.TASK_REPEAT.F.SATURDAY,
            },
          },
          {
            key: 'sunday',
            type: 'checkbox',
            resetOnHide: false,
            templateOptions: {
              label: T.F.TASK_REPEAT.F.SUNDAY,
            },
          },
        ],
      },
    ],
  },
  // REPEAT CFG END
];

export const TASK_REPEAT_CFG_ADVANCED_FORM_CFG: FormlyFieldConfig[] = [
  {
    key: 'defaultEstimate',
    type: 'duration',
    templateOptions: {
      label: T.F.TASK_REPEAT.F.DEFAULT_ESTIMATE,
      description: T.G.DURATION_DESCRIPTION,
    },
    // otherwise the input duration field messes up :(
    modelOptions: {
      updateOn: 'blur',
    },
  },

  {
    key: 'notes',
    type: 'textarea',
    templateOptions: {
      label: T.F.TASK_REPEAT.F.NOTES,
      rows: 4,
    },
  },
  // Schedule type: from due date or from completion
  {
    key: 'repeatFromCompletionDate',
    type: 'select',
    defaultValue: false,
    resetOnHide: false,
    hideExpression: (model: any) => {
      // Only show for custom settings with intervals > 1
      if (model.quickSetting !== 'CUSTOM') {
        return true;
      }
      return false;
    },
    templateOptions: {
      label: T.F.TASK_REPEAT.F.SCHEDULE_TYPE_LABEL,
      options: [],
    },
    expressionProperties: {
      ['templateOptions.options']: (model: any) => {
        const repeatEvery = model.repeatEvery || 1;
        const cycleMap: Record<string, string> = {
          DAILY: repeatEvery === 1 ? 'day' : 'days',
          WEEKLY: repeatEvery === 1 ? 'week' : 'weeks',
          MONTHLY: repeatEvery === 1 ? 'month' : 'months',
          YEARLY: repeatEvery === 1 ? 'year' : 'years',
        };
        const cycleName = cycleMap[model.repeatCycle] || 'days';

        return [
          {
            value: false,
            label: `Fixed schedule (every ${repeatEvery} ${cycleName} from start date)`,
          },
          {
            value: true,
            label: `After completion (${repeatEvery} ${cycleName} after I finish)`,
          },
        ];
      },
    },
  },
  {
    key: 'shouldInheritSubtasks',
    type: 'checkbox',
    defaultValue: false,
    templateOptions: {
      label: T.F.TASK_REPEAT.F.INHERIT_SUBTASKS,
      description: T.F.TASK_REPEAT.F.INHERIT_SUBTASKS_DESCRIPTION,
    },
  },
  // child option depending on inherit
  {
    key: 'disableAutoUpdateSubtasks',
    type: 'checkbox',
    defaultValue: false,
    hideExpression: (model: any) => !model.shouldInheritSubtasks,
    templateOptions: {
      label: T.F.TASK_REPEAT.F.DISABLE_AUTO_UPDATE_SUBTASKS,
      description: T.F.TASK_REPEAT.F.DISABLE_AUTO_UPDATE_SUBTASKS_DESCRIPTION,
    },
    className: 'sp-formly-child-option',
  },
  {
    key: 'waitForCompletion',
    type: 'checkbox',
    defaultValue: false,
    templateOptions: {
      label: T.F.TASK_REPEAT.F.WAIT_FOR_COMPLETION,
      description: T.F.TASK_REPEAT.F.WAIT_FOR_COMPLETION_DESCRIPTION,
    },
  },
  {
    key: 'skipOverdue',
    type: 'checkbox',
    defaultValue: false,
    templateOptions: {
      label: T.F.TASK_REPEAT.F.SKIP_OVERDUE,
      description: T.F.TASK_REPEAT.F.SKIP_OVERDUE_DESCRIPTION,
    },
  },
];
