import { BuildUrlUtils } from  './../../../utils/build-url';

'use strict';

describe('Controller: OverallInsightsEditorCtrl', function() {

    var overallInsightEditorController;
    var scope, $httpBackend, CurrentSession, stateparams, $controller, $log, Insight, User, RealtimeProductResource, notify, createController;
    var spy;


    // Initialize the controller and a mock scope
    beforeEach(inject(function(
            _$controller_, 
            $rootScope,
            _CurrentSession_,
       		_$httpBackend_,
            _$log_,
            _Insight_, 
            _User_, 
            _RealtimeProductResource_, 
            _notify_
        ) {

        var product;
        $controller = _$controller_;
        $httpBackend = _$httpBackend_;
		CurrentSession = _CurrentSession_;
        $log = _$log_;
        Insight = _Insight_ ;
        User = _User_ ;
        RealtimeProductResource = _RealtimeProductResource_ ;
        notify = _notify_;
        product = {
            _id: 'aabbcc',
            insightsDraft: [{
                'createdAt': '2015-03-16T11:03:25.901Z',
                'text': 'Insight 1',
                'reviewIds': [
                    '54f8d13ee365e512625ed487',
                    '54f8d13ee365e512625ed491',
                ],
                'isHidden': false
            }, {
                'createdAt': '2015-03-16T11:03:25.901Z',
                'text': 'Insight 2',
                'reviewIds': [

                    '54f8d13ee365e512625ed4b2',
                    '54f8d13ee365e512625ed4b8',
                    '54f8d13ee365e512625ed4c4',
                    '54f8d13ee365e512625ed4d0',
                    '54fb0534034c18ac0bef3db3'
                ],
                'isHidden': false
            }, {
                'createdAt': '2015-03-16T11:03:25.901Z',
                'text': 'Insight 3',
                'reviewIds': [
                    '54fb0535034c18ac0bef3dd4',
                    '54fb0535034c18ac0bef3dde',
                    '54fb0535034c18ac0bef3de9',
                    '54fb0535034c18ac0bef3dff',
                    '54fb0535034c18ac0bef3e00',
                    '54fb0535034c18ac0bef3e08'
                ],
                'isHidden': true
            }]
        };

        CurrentSession.setCurrentProduct(product);

        scope = $rootScope.$new();


        //mock your stateparams object with your id
        // stateparams = { productId : product._id };
        // $stateParams: stateparams


        createController = function() {
            scope.currentProduct = product;
            var ctrl = $controller('OverallInsightEditorCtrl', {
                '$scope': scope
            });
            return ctrl;
        };

        scope.$digest();
        
        
    }));


    it('should remove an insights  correctly', function() {
        var query, url, putBody;
        window.confirm = function() {
            return true;
        };

        overallInsightEditorController = createController();

        spy = sinon.spy(RealtimeProductResource, 'removeInsightByProductAndInsight');	

        scope.removeInsight(scope.currentProduct.insightsDraft[0]);
        expect(spy).to.have.been.called;

    });

    it('should link a review to an insight correctly', function() {
        var query, url, putBody;

        overallInsightEditorController = createController();

        spy = sinon.spy(RealtimeProductResource, 'linkInsightByProductAndInsightAndReviewIdAndIsCallCenterData');
        
        scope.review = {
            _id: 'new ID',
            review: {
                channel: 'philips-call-center'
            }
        };
        scope.linkInsight(scope.currentProduct.insightsDraft[0]);
        expect(spy).to.have.been.called;

    });

    it('should unlink a review to an insight correctly', function() {
        var query, url, putBody;

        scope.review = {
            _id: 'new ID',
            review: {
                channel: 'philips-call-center'
            }
        };

        overallInsightEditorController = createController();

        spy = sinon.spy(RealtimeProductResource, 'unlinkInsightByProductAndInsightAndReviewIdAndIsCallCenterData');

        scope.unlinkInsight(scope.currentProduct.insightsDraft[0]);
        expect(spy).to.have.been.called;
    });

    it('should toggle a link of a review for an insight correctly', function() {
        var query, url, putBody;

        scope.review = {
            _id: 'new ID',
            review: {
                channel: 'philips-call-center'
            }
        };

        overallInsightEditorController = createController();

        var spy1 = sinon.spy(RealtimeProductResource, 'unlinkInsightByProductAndInsightAndReviewIdAndIsCallCenterData');
        var spy2 = sinon.spy(RealtimeProductResource, 'linkInsightByProductAndInsightAndReviewIdAndIsCallCenterData');

        scope.toggleLink(scope.currentProduct.insightsDraft[0]);
        expect(spy1.callCount + spy2.callCount).to.equal(1);

    });
    it('should add a new insight correctly', function() {
        var url, putBody;
        // var isoString = new Date(2014, 0, 1, 2, 0).toISOString();
        // var OldDate = window.Date;
        // window.Date = function() {
        //     return new OldDate(2014, 0, 1, 2, 0);
        // };

        scope.review = {
            _id: 'new ID',
            review: {
                channel: 'philips-call-center'
            }
        };
        overallInsightEditorController = createController();
        spy = sinon.spy(RealtimeProductResource, 'addInsightByProductAndInsight');
        scope.addNewInsight('text of new added insight');

        expect(spy).to.have.been.called;
    });

    it('should determine if a review is linked to an insight correctly', function() {

        scope.review = {
            _id: 'new ID',
            review: {
                channel: 'philips-call-center'
            }
        };

        overallInsightEditorController = createController();

        expect(scope.isLinked(scope.currentProduct.insightsDraft[0], 'new ID')).to.be.eql(false);

        scope.currentProduct.insightsDraft[0].callCenterDataIds = ['new ID'];

        expect(scope.isLinked(scope.currentProduct.insightsDraft[0], 'new ID')).to.be.eql(true);

    });
});
