import {competitionConstants} from '../_constants';
import {competitionService} from '../_services';
import {Competition} from '../_helpers';

import {push} from 'react-redux';

export const competitionActions = {
  getAll,
  get,
  getCategories,
  getMembers,
  setMembers,
  getCatches,
  getResults,
  save
};

function getResults(id, competition_id) {
  return dispatch => {
    dispatch(request());
    return competitionService.getResults(id, competition_id).then(results => dispatch(success(results)), error => dispatch(failure(error)));
  };

  function request() {
    return {type: competitionConstants.GET_RESULTS_REQUEST}
  }
  function success(results) {
    return {type: competitionConstants.GET_RESULTS_SUCCESS, results}
  }
  function failure(error) {
    return {type: competitionConstants.GET_RESULTS_FAILURE, error}
  }
}

function getCatches(id) {
  return dispatch => {
    dispatch(request());
    return competitionService.getCatches(id).then(catches => dispatch(success(catches)), error => dispatch(failure(error)));
  };

  function request() {
    return {type: competitionConstants.GET_CATCHES_REQUEST}
  }
  function success(catches) {
    return {type: competitionConstants.GET_CATCHES_SUCCESS, catches}
  }
  function failure(error) {
    return {type: competitionConstants.GET_CATCHES_FAILURE, error}
  }
}

function getMembers(id) {
  return dispatch => {
    dispatch(request());
    return competitionService.getMembers(id).then(members => dispatch(success(members)), error => dispatch(failure(error)));
  };

  function request() {
    return {type: competitionConstants.GET_MEMBERS_REQUEST}
  }
  function success(members) {
    return {type: competitionConstants.GET_MEMBERS_SUCCESS, members}
  }
  function failure(error) {
    return {type: competitionConstants.GET_MEMBERS_FAILURE, error}
  }
}

function setMembers(id, members) {
  return dispatch => {
    dispatch(request());
    return competitionService.setMembers(id, members).then(members => dispatch(success(members)), error => dispatch(failure(error)));
  };

  function request() {
    return {type: competitionConstants.SET_MEMBERS_REQUEST}
  }
  function success(members) {
    return {type: competitionConstants.SET_MEMBERS_SUCCESS, members}
  }
  function failure(error) {
    return {type: competitionConstants.SET_MEMBERS_FAILURE, error}
  }
}

function getCategories() {
  return dispatch => {
    dispatch(request());
    return competitionService.getCategories().then(categories => dispatch(success(categories)), error => dispatch(failure(error)));
  };

  function request() {
    return {type: competitionConstants.GET_CATEGORIES_REQUEST}
  }
  function success(categories) {
    return {type: competitionConstants.GET_CATEGORIES_SUCCESS, categories}
  }
  function failure(error) {
    return {type: competitionConstants.GET_CATEGORIES_FAILURE, error}
  }
}

function getAll() {
  return dispatch => {
    dispatch(request());
    return competitionService.getAll().then(competitions => dispatch(success(competitions)), error => dispatch(failure(error)));
  };

  function request() {
    return {type: competitionConstants.GETALL_REQUEST}
  }
  function success(competitions) {
    return {type: competitionConstants.GETALL_SUCCESS, competitions}
  }
  function failure(error) {
    return {type: competitionConstants.GETALL_FAILURE, error}
  }
}

function save(competition) {
  return dispatch => {
    dispatch(request());
    return competitionService.save(competition).then(competition => dispatch(success(competition)), error => dispatch(failure(error)));
  };
  function request() {
    return {type: competitionConstants.SAVE_REQUEST, competition}
  }
  function success(competition) {
    return {type: competitionConstants.SAVE_SUCCESS, competition}
  }
  function failure(error) {
    return {type: competitionConstants.SAVE_FAILURE, error}
  }
}

function get(id) {

  return dispatch => {
    dispatch(request());
    return competitionService.get(id).then(competition => dispatch(success(competition)), error => dispatch(failure(error)));
  };

  function request() {
    return {type: competitionConstants.GET_REQUEST}
  }
  function success(competition) {
    return {type: competitionConstants.GET_SUCCESS, competition}
  }
  function failure(error) {
    return {type: competitionConstants.GET_FAILURE, error}
  }
}
