angular
        .module('carfriend')
        .controller('ChatDetailCtrl', ChatDetailCtrl);

function ChatDetailCtrl($stateParams, $timeout, $ionicScrollDelegate, ChatService, AuthService, $filter, Backand, $cordovaCamera, $scope, $state, $ionicHistory, $ionicNavBarDelegate) {
    var vm = this;
    
    vm.newMessage = {};
    vm.messages = {};
    vm.currentUser = AuthService.getUser();
    
    vm.sendMessage = sendMessage;
    vm.inputUp = inputUp;
    vm.inputDown = inputDown;
    vm.makePhoto = makePhoto;
        
    loadCarversationDetails();
    $scope.$on('$ionicView.enter', function(){
        getMessages(10); 
    })
       
    
    Backand.on('new_message', function (data) {
        getMessages(20);
    });
    
   
    ////////////////////
      
    
    function loadCarversationDetails(){
        vm.currentChat = {
            username:$stateParams.user.username,
            otherUser: $stateParams.user.userid,
            photo: $stateParams.user.userType === "driver" ? $stateParams.user.photoCar : $stateParams.user.photoUser,
            email: $stateParams.user.email,
            id: $stateParams.chatId,
            currentUser: vm.currentUser.details.id
        };
        console.log("CUREnt ", vm.currentChat);
    }
    ;
    
    function getMessages(kolko){
        if ($filter('isEmpty')(vm.messages)) {
            vm.loadingMessages = true;
        }
        ChatService.getMessages($stateParams.chatId, kolko).then(function(messages){
            console.log(messages)
            vm.loadingMessages = false;
            vm.messages = messages.data.data;
            $ionicScrollDelegate.$getByHandle('chatScroll').scrollBottom(true);
        });
    }
    
    function sendMessage() {
        if (vm.newMessage.text !== '' || vm.newMessage.image !== '') {
            vm.newMessage.chats = $stateParams.chatId;
            vm.newMessage.user = vm.currentUser.details.id;
            vm.newMessage.datetime = $filter('date')(new Date(), 'yyyy-MM-dd HH:mm:ss');
            ChatService.post(vm.newMessage, vm.currentChat.email).then(function (succ) {
                vm.newMessage = {};
                getMessages();
            });
        }
    }
    
        function makePhoto() {
        var options = {
            quality: 80,
            destinationType: Camera.DestinationType.DATA_URL,
            sourceType: Camera.PictureSourceType.CAMERA,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 500,
            targetHeight: 500,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: false,
            allowEdit: false,
            correctOrientation: true
        };
        $cordovaCamera.getPicture(options).then(function (imageData) {
            vm.newMessage.image = "data:image/jpeg;base64," + imageData;
            sendMessage();
        }, function (err) {
          
        });
    }
    ;

    function inputUp() {
        $timeout(function () {
            $ionicScrollDelegate.$getByHandle('chatScroll').scrollBottom(true);
        }, 100);
    }

    function inputDown() {
        $ionicScrollDelegate.$getByHandle('chatScroll').resize();
    }
}

