import { observable } from 'mobx';
import { computed } from 'mobx';
import { invoke } from 'lodash';
import moment from 'moment';
import Time from '../time';
import {
  BaseModel, identifiedBy, field, identifier,
} from 'shared/model';

export default
@identifiedBy('student/task')
class StudentTask extends BaseModel {

  @observable hidden = false;

  @identifier id;
  @field title;
  @field type;
  @field complete;
  @field is_deleted;
  @field is_college;
  @field complete_exercise_count = 0;
  @field correct_exercise_count;
  @field exercise_count = 0;
  @field completed_accepted_late_exercise_count = 0;
  @field completed_on_time_exercise_count = 0;

  @field steps_count = 0;
  @field completed_steps_count = 0;
  @field completed_on_time_steps_count = 0;
  @field completed_accepted_late_steps_count = 0;

  @field({ type: 'date' }) last_worked_at;
  @field({ type: 'date' }) due_at;
  @field({ type: 'date' }) opens_at;
  @field({ type: 'date' }) accepted_late_at;

  @computed get workedLate() {
    return moment(this.last_worked_at).isAfter(this.due_at);
  }

  @computed get isPastDue() {
    return moment(this.due_at).isBefore(Time.now);
  }

  @computed get isAlmostDue() {
    return moment(Time.now).isBetween(
      moment(this.due_at).subtract(1, 'day'),
      this.due_at,
    );
  }

  @computed get scoreShown() {
    return Boolean(this.isPastDue && this.complete);
  }

  @computed get lateWorkIsAccepted() {
    return Boolean(this.accepted_late_at);
  }

  @computed get isHomework() {
    return 'homework' === this.type;
  }

  @computed get isReading() {
    return 'reading' === this.type;
  }

  @computed get isStarted() {
    return this.completed_steps_count > 0;
  }

  @computed get canWork() {
    //students cannot work or view a task if it has been deleted and they haven't started it
    return Boolean(
      this.opens_at < Time.now && !(
        this.is_deleted &&
          this.complete_exercise_count === 0
      )
    );
  }

  // called from API
  hide() {}
  onHidden() {
    this.hidden = true;
  }
};

@@ -230,16 +230,471 @@
 odel';%0A%0A
+%0Aconst StudentTaskFeedback = %7B%0A  homework(t) %7B%0A    if (!t.isStarted) %7B return 'Not started'; %7D%0A    return t.scoreShown ?%0A      %60$%7Bt.correct_exercise_count%7D/$%7Bt.exercise_count%7D correct%60 :%0A      %60$%7Bt.complete_exercise_count%7D/$%7Bt.exercise_count%7D answered%60;%0A  %7D,%0A  reading(t) %7B%0A    if (!t.isStarted) %7B return 'Not started'; %7D%0A    return t.complete ? 'Complete' : 'In progress';%0A  %7D,%0A  external(t) %7B%0A    return t.complete ? 'Clicked' : 'Not started';%0A  %7D,%0A%7D;%0A%0A
 export d
@@ -2428,16 +2428,120 @@
 );%0A  %7D%0A%0A
+  @computed get studentFeedback() %7B%0A    return invoke(StudentTaskFeedback, this.type, this) %7C%7C '';%0A  %7D%0A%0A
   // cal

add logic for feedback