var peristalsis = false;
var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.itemInit();
        this.eventInit();
        this.otherInit();
    },
    itemInit:function() {
//        var today = new Date();
//        $("#picker").datepicker({
//            dateFormat: "yy년 mm월 dd일"
//        });
//        $("#picker").datepicker("setDate",today);
//
//        if ($('#prevDay').hasClass('selected')) {
//            date.setTime(date.getTime() - (1000*60*60*24));
//            $("#picker").datepicker("setDate", date);
//        }
//        app.getTimeTotal($("#picker").val());
        app.setToday();
    },
    eventInit:function(){
        //다음날 버튼
        $('.last').on("click", function () {
            var date = $('#picker').datepicker('getDate');
            date.setTime(date.getTime() + (1000*60*60*24));
            $("#picker").datepicker("setDate", date);
            app.getTimeTotal($("#picker").val());
            app.initDayButton();
        });
        //전날 버튼
        $('.first').on("click", function () {
            var date = $('#picker').datepicker('getDate');
            date.setTime(date.getTime() - (1000*60*60*24));
            $("#picker").datepicker("setDate", date);
            app.getTimeTotal($("#picker").val());
            app.initDayButton();

        });
        $('#todayTotal').on("click", function(){
            var today = new Date();
            $("#picker").datepicker({
                dateFormat: "yy년 mm월 dd일"
            });
            $("#picker").datepicker("setDate",today);
            app.getTimeTotal($("#picker").val());
            app.initDayButton();
        });
    },
    otherInit:function(){
        if(isIphone()){

        }else {
            document.addEventListener("backbutton", app.backbuttonAction, false);
        }
    },
    backbuttonAction:function(){
        if(isIphone()){
           cordova.plugins.finPlugin.backButton("", null, null);
       }else {
           cordova.plugins.finPlugin.moveStack(MAIN.DATA,null, null);
       }
    },
    getTimeTotal:function(today){
        
        if(isIphone())
        {
            var dateVal = {"calender" : today};
            cordova.plugins.finPlugin.getTimeTotal(dateVal, function(data){
                app.setPaymentData(data);
            }, null);
        }else
        {
            cordova.plugins.finPlugin.getTimeTotal(today, function(data){
                app.setPaymentData(data);
            }, null);
        }
        
        
    },
    setPaymentData:function(data) {
        
        if(isIphone())
        {
            
            if(data == null) return;
            
            data = JSON.parse(data);
            
            var dataArray = data['hourlysList'];
            var total = data['hourlyTotal'];
        
            
             if(dataArray == undefined || dataArray.length == 0) {
                 $("#dv_list").html('');
                 return;
             }
            
            
            var html_data = "<ul class='SalesWrap01'> <li>합계</li>";
            for(var z=0; z<24; z++)
            {
                if(z<10)
               {
                   html_data = html_data + "<li> 0" + z +":00 </li>"
               }else
               {
                   html_data = html_data + "<li> " + z +":00 </li>"
               }
                           
            }
           
            
            html_data = html_data + "</ul > <ul class='SalesWrap02'> <li>" + total +"</li>";
            
             while (dataArray.length > 0)
             {
                 var dataObject = JSON.parse(dataArray.shift());
                 var valuer = dataObject['hour'];
                 
                 html_data = html_data + "<li>" + valuer + "</li>";
             }
            html_data = html_data + "</ul >";
            $("#dv_list").html(html_data);
            
        }else
        {
            if(data == null) return;
            var dataObject = JSON.parse(data);
            if(dataObject == null) return;
            var dataObj = dataObject.DATA;
            var html_data = "<ul class='SalesWrap01'> <li>합계</li>";
            for (var i = 0; i < 24; i++) {
                for(key in dataObj) {
                    var value = parseInt(key.substring(0, 2));
                    if (i == value) {
                        //alert('key:' + key + ' / ' + 'value:' + dataObj[key]);
                        html_data = html_data + "<li>" + key + "</li>";
                        break;
                    }
                }
            }
            html_data = html_data + "</ul > <ul class='SalesWrap02'> <li>" + dataObject.TOTAL +"</li>";
            for (var i = 0; i < 24; i++) {
                for(key in dataObj) {
                    var value = parseInt(key.substring(0, 2));
                    if (i == value) {
                        //alert('key:' + key + ' / ' + 'value:' + dataObj[key]);
                        html_data = html_data + "<li>" + dataObj[key] + "</li>";
                        break;
                    }
                }
            }
            html_data = html_data + "</ul >";
            $("#dv_list").html(html_data);
        }
        
        
    },
    initDayButton:function(){
        $('#thisDay').addClass('selected');
        $('#prevDay').removeClass('selected');
    },
    setToday:function(){
        var today = new Date();
        $("#picker").datepicker({
            dateFormat: "yy년 mm월 dd일"
        });
        $("#picker").datepicker("setDate",today);
        app.getTimeTotal($("#picker").val());
        app.initDayButton();
    },
    setThisDay:function(){
        $('#thisDay').addClass('selected');
        $('#prevDay').removeClass('selected');
        app.getTimeTotal($("#picker").val());
    },
    setPrevDay:function(){
        $('#thisDay').removeClass('selected');
        $('#prevDay').addClass('selected');
        app.getTimeTotal(app.getPrevDay($('#picker').datepicker('getDate')));
    },
    getPrevDay:function(today){
        today.setTime(today.getTime() - (1000*60*60*24));
        var year = today.getYear().toString();
        var month = today.getMonth() + 1;
        var day = today.getDate();
        return year.substring(1) + '년 ' + month + '월 ' + day + '일';
    }
};

app.initialize();