import { Action, Module, Mutation, VuexModule } from 'vuex-module-decorators'
import {
  Category,
  CategoryCounting,
  CategoryGroup,
  CategoryGroupCounting,
} from '@/widgets/tasks/types/Category'
import { NotifyHelper } from '@/helpers'
import CategoryService from '@/widgets/tasks/http/services/CategoryService'
import WebsocketService from '@/widgets/tasks/http/services/WebsocketService'
import { Manager } from '@/types/Manager'

@Module({
  name: 'CategoryModule',
  namespaced: true,
})
export class CategoryModule extends VuexModule {
  categories: Category[] = []
  counting: CategoryGroupCounting | null = null
  currentGroup: CategoryGroup = 'today'

  get totalCount(): number {
    if (!this.counting) {
      return 0
    }

    return Object.values(this.counting).reduce((total, group) => {
      return total + group.allCount
    }, 0)
  }

  get getCountTaskForCategory() {
    return (group: CategoryGroup, categoryId: number) => {
      if (!this.counting) {
        return 0
      }

      const counting = this.counting[group].groupCount.find(
        c => c.id === categoryId
      ) as CategoryCounting

      return counting.taskCount
    }
  }

  get getCountTasksForGroup() {
    return (group: CategoryGroup) =>
      this.counting ? this.counting[group].allCount : 0
  }

  get getGroupTitle() {
    return (groupName: CategoryGroup) =>
      this.counting ? this.counting[groupName].title : ''
  }

  get getCategoryById() {
    return (categoryId: number) =>
      this.categories.find(category => category.id === categoryId)
  }

  @Mutation
  setCurrentGroup(group: CategoryGroup) {
    this.currentGroup = group
  }

  @Mutation
  setCategories(categories: Category[]) {
    this.categories = categories
  }

  @Mutation
  setCounting(counting: CategoryGroupCounting) {
    this.counting = counting
  }

  @Action
  loadCategories() {
    return CategoryService.loadCategories()
      .then(categories => this.setCategories(categories))
      .catch(() => NotifyHelper.error('Ошибка загрузки категорий'))
  }

  @Action
  loadCounting() {
    return CategoryService.loadCounting().then(counting =>
      this.setCounting(counting)
    )
  }

  @Action
  initWebsocket(payload: { authToken: string; user: Manager }) {
    try {
      WebsocketService.init(payload.authToken, payload.user)

      WebsocketService.onUpdateCounting(payload => {
        this.setCounting(payload.counting)
      })
    } catch (e) {
      NotifyHelper.error('Ошибка подключения к сокетам')
    }
  }
}
