/*
 * Copyright (C) 2019 Robert Bosch GmbH Copyright (C) 2019 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { Component } from '@angular/core';

export interface Task {
  name: string;
  completed: boolean;
  subtasks?: Task[];
}

@Component({
  selector: 'bci-mat-checkbox-demo-nested-checklist',
  styles: [`.mat-mdc-checkbox {
    margin-bottom: 8px;
  }`],
  templateUrl: 'nested-checklist.html',
})
export class MatCheckboxDemoNestedChecklistComponent {
  tasks: Task[] = [
    {
      name: 'All',
      completed: false,
      subtasks: [
        { name: 'Draft', completed: false },
        { name: 'In Progress', completed: false },
        { name: 'Submitted', completed: false },
        { name: 'Approved', completed: false },
        { name: 'Rejected', completed: false },
      ],
    },
    {
      name: 'Groceries',
      completed: false,
      subtasks: [
        { name: 'Organic Eggs', completed: false },
        { name: 'Protein Powder', completed: false },
        { name: 'Almond Meal Flour', completed: false },
      ],
    },
  ];

  allComplete(task: Task): boolean {
    const subtasks = task.subtasks;

    if (!subtasks) {
      return false;
    }

    return subtasks.every((t) => t.completed) ? true : subtasks.every((t) => !t.completed) ? false : task.completed;
  }

  someComplete(tasks: Task[]): boolean {
    const numComplete = tasks.filter((t) => t.completed).length;
    return numComplete > 0 && numComplete < tasks.length;
  }

  setAllCompleted(tasks: Task[], completed: boolean) {
    tasks.forEach((t) => (t.completed = completed));
  }
}

@Component({
  selector: 'bci-mat-checkbox-demo',
  templateUrl: 'checkbox-demo.html',
  styleUrls: ['checkbox-demo.scss'],
})
export class CheckboxDemoComponent {
  isIndeterminate = false;
  isChecked = false;
  isDisabled = false;
  alignment = 'start';
  useAlternativeColor = false;

  printResult() {
    if (this.isIndeterminate) {
      return 'Maybe!';
    }
    return this.isChecked ? 'Yes!' : 'No!';
  }

  checkboxColor() {
    return this.useAlternativeColor ? 'primary' : 'accent';
  }
}
