'use strict';

var createStore = require('fluxible/utils/createStore');
var makeHandlers = require('../lib/fluxible/makeHandlers');
var BusinessEvents = require('../constants/BusinessConstants').Events;
var BusinessActions = require('../actions/Business');
var HairdresserEvents = require('../constants/HairdresserConstants').Events;
var Notify = require('../actions/Flash/Notify');
var _ = require('lodash');

module.exports = createStore({
    storeName: 'BusinessStore',
    handlers: makeHandlers({
        handleReceive: BusinessEvents.RECEIVE,
        handleReceiveSuccess: BusinessEvents.RECEIVE_SUCCESS,
        handleReceiveFailure: BusinessEvents.RECEIVE_FAILURE,
        handleReceiveHairdressersSuccess: BusinessEvents.RECEIVE_HAIRDRESSERS_SUCCESS,
        handleHairdresserSaveSuccess: HairdresserEvents.SAVE_SUCCESS
    }),
    initialize: function () {
        this.businesses = {};
        this.hairdressers = null;
    },
    dehydrate: function () {
        return {
            business    : this.business,
            businesses  : this.businesses
        };
    },
    rehydrate: function (state) {
        this.business = state.business;
        this.businesses = state.businesses;
    },
    handleReceive: function (payload) {
        this.businesses[payload.id] = _.assign({}, this.businesses[payload.id], {
            loading: true
        });
        this.emitChange();
    },
    handleReceiveSuccess: function (payload) {
        this.businesses[payload.id] = _.assign({}, this.businesses[payload.id], {
            entity  : payload.business,
            loading : false
        });

        if (this.business && payload.business.id == this.business.id) {
            this.business = payload.business;
            this.uploadInProgress = false;
        }

        this.emitChange();
    },
    handleReceiveFailure: function (payload) {
        this.businesses[payload.id] = _.assign({}, this.businesses[payload.id], {
            loading: false
        });
        this.emitChange();
    },
    handleReceiveHairdressersSuccess: function (payload) {
        if (!this.business || payload.business.id != this.business.id) {
            return;
        }

        this.hairdressers = payload.hairdressers;
        this.emitChange();
    },
    handleHairdresserSaveSuccess: function (payload) {
        var hairdresser = payload.hairdresser;
        if (!this.business || hairdresser.business.id != this.business.id) {
            return;
        }

        var index = _.findIndex(this.hairdressers, {id: hairdresser.id});
        if (-1 == index) {
            this.hairdressers.push(hairdresser);
        } else {
            this.hairdressers[index] = hairdresser;
        }

        this.emitChange();
    },
    getHairdressers: function () {
        if (this.business && !this.hairdressers) {
            this.dispatcher.getContext().executeAction(BusinessActions.RefreshHairdressers, {
                business: this.business
            });
        }

        return this.hairdressers || [];
    },
    // TODO: move discount code into a discount store
    getDiscountForBusiness: function(businessId) {
        var business = this.getById(businessId);
        if (!business) return;

        var max =  _.chain(business.timetable)
            .map(function(day) {return _.max(_.compact(_.pluck(day, 'discount'))) })
            .max()
            .value();
        var discountsAvailable = _.chain(business.timetable)
            .reduce(function(result, timetable, day) {
                var values = _.compact(_.pluck(timetable, 'discount'));
                if(values.length > 0) {
                    _.each(values, function(value) {
                        if(result[value]) {
                            result[value].push(day);
                        } else {
                            result[value] = [day];
                        }
                    })
                }
                return result;
            }, {})
            .value();
        var discountObj = {
            max: _.isFinite(max) ? max : null,
            discountsAvailable: discountsAvailable
        };
        return discountObj;
    },
    getById: function (businessId) {
        var business = this.businesses[businessId];

        if (typeof business == 'undefined') {
            this._loadById(businessId);
        }

        return business && business.entity;
    },
    _loadById: function (businessId) {
        this.dispatcher.getContext().executeAction(BusinessActions.Fetch, {
            id: businessId
        });
    }
});

@@ -669,166 +669,9 @@
 LURE
-,%0A        handleReceiveHairdressersSuccess: BusinessEvents.RECEIVE_HAIRDRESSERS_SUCCESS,%0A        handleHairdresserSaveSuccess: HairdresserEvents.SAVE_SUCCESS
 %0A
+
     
@@ -738,42 +738,8 @@
 %7B%7D;%0A
-        this.hairdressers = null;%0A
     
@@ -811,53 +811,10 @@
 ness
-    : this.business,%0A            businesses  
+es
 : th
@@ -883,48 +883,8 @@
 ) %7B%0A
-        this.business = state.business;%0A
     
@@ -1700,32 +1700,32 @@
 lse%0A        %7D);%0A
+
         this.emi
@@ -1746,1023 +1746,8 @@
  %7D,%0A
-    handleReceiveHairdressersSuccess: function (payload) %7B%0A        if (!this.business %7C%7C payload.business.id != this.business.id) %7B%0A            return;%0A        %7D%0A%0A        this.hairdressers = payload.hairdressers;%0A        this.emitChange();%0A    %7D,%0A    handleHairdresserSaveSuccess: function (payload) %7B%0A        var hairdresser = payload.hairdresser;%0A        if (!this.business %7C%7C hairdresser.business.id != this.business.id) %7B%0A            return;%0A        %7D%0A%0A        var index = _.findIndex(this.hairdressers, %7Bid: hairdresser.id%7D);%0A        if (-1 == index) %7B%0A            this.hairdressers.push(hairdresser);%0A        %7D else %7B%0A            this.hairdressers%5Bindex%5D = hairdresser;%0A        %7D%0A%0A        this.emitChange();%0A    %7D,%0A    getHairdressers: function () %7B%0A        if (this.business && !this.hairdressers) %7B%0A            this.dispatcher.getContext().executeAction(BusinessActions.RefreshHairdressers, %7B%0A                business: this.business%0A            %7D);%0A        %7D%0A%0A        return this.hairdressers %7C%7C%C2%A0%5B%5D;%0A    %7D,%0A
     

Remove unused methods