import { createStore, applyMiddleware, combineReducers, compose } from 'redux';
import promiseMiddleware from 'redux-promise';
import thunkMidleware from "redux-thunk";

import PulseListApp from './containers/PulseListApp.jsx';
import PulseEditApp from './containers/PulseEditApp.jsx';

import * as reducers from './reducers';

const finalCreateStore = compose(
  applyMiddleware(
      thunkMidleware,
      promiseMiddleware
  ),
  createStore
);

const reducer = combineReducers(reducers);

var Pulse = angular.module('metabase.pulse', []);

Pulse.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/pulse', {
        template: '<div mb-redux-component class="flex flex-column flex-full" />',
        controller: ['$scope', '$location', '$route', '$routeParams',
            function($scope, $location, $route, $routeParams) {
                $scope.Component = PulseListApp;
                $scope.props = {
                    onChangeLocation: function(url) {
                        $scope.$apply(() => $location.url(url));
                    }
                };
                $scope.store = finalCreateStore(reducer, {});
            }
        ]
    });

    $routeProvider.when('/pulse/create', {
        template: '<div mb-redux-component class="flex flex-column flex-full" />',
        controller: ['$scope', '$location', '$route', '$routeParams',
            function($scope, $location, $route, $routeParams) {
                $scope.Component = PulseEditApp;
                $scope.props = {
                    onChangeLocation: function(url) {
                        $scope.$apply(() => $location.url(url));
                    }
                };
                $scope.store = finalCreateStore(reducer, {});
            }
        ]
    });

    $routeProvider.when('/pulse/:pulseId', {
        template: '<div mb-redux-component class="flex flex-column flex-full" />',
        controller: ['$scope', '$location', '$route', '$routeParams',
            function($scope, $location, $route, $routeParams) {
                $scope.Component = PulseEditApp;
                $scope.props = {
                    pulseId: parseInt($routeParams.pulseId),
                    onChangeLocation: function(url) {
                        $scope.$apply(() => $location.url(url));
                    }
                };
                $scope.store = finalCreateStore(reducer, {});
            }
        ]
    });
}]);

@@ -1170,33 +1170,174 @@
      %7D%0A        %5D
+,%0A        resolve: %7B%0A            appState: %5B%22AppState%22, function(AppState) %7B%0A                return AppState.init();%0A            %7D%5D%0A        %7D
 %0A
-
     %7D);%0A%0A    $ro
@@ -1908,32 +1908,173 @@
      %7D%0A        %5D
+,%0A        resolve: %7B%0A            appState: %5B%22AppState%22, function(AppState) %7B%0A                return AppState.init();%0A            %7D%5D%0A        %7D
 %0A    %7D);%0A%0A    $r
@@ -2700,32 +2700,32 @@
 ;%0A            %7D%0A
-
         %5D%0A    %7D)
@@ -2717,16 +2717,157 @@
        %5D
+,%0A        resolve: %7B%0A            appState: %5B%22AppState%22, function(AppState) %7B%0A                return AppState.init();%0A            %7D%5D%0A        %7D
 %0A    %7D);

add a resolve on our pulse routes to ensure we have access to the logged in user in our controller.