$(function() {
    'use strict';
    var searchView = BWA.SearchView = BWA.SearchView || {};

    var day = 'day',
        days = 'days',
        month = 'month',
        months = 'months',
        year = 'year',
        years = 'years';

    // hcLee 2016 04 04
    function dateBySubtract2(date, number, unit) {
        return moment(date).subtract(number, unit).toDate();
    }

    function dateBySubtract(number, unit) {
        return moment().subtract(number, unit).toDate();
    }

    function dateStartOf(unit) {
        return moment().startOf(unit).toDate();
    }

    // hcLee 2016 04 04
    function dateStartOf2(date, unit) {
        return moment(date).startOf(unit).toDate();
    }

    function now() {
        return new Date();
    }

    function HourBox() {
        this.startDate = ko.observable();
        this.startTime = ko.observable();
        this.endDate = ko.observable();
        this.endTime = ko.observable();
        /*
        this.startDate = new Date();
        this.startTime = new Date();
        this.endDate = new Date();
        this.endTime = new Date();*/

        this.timeChange = function () {
            alert('1');
        }.bind(this);

        this.set24hours = function () {
            //this.startDate(dateBySubtract(1, days));
            this.startDate(dateBySubtract2(this.endDate(), 1, days));
            this.startTime(this.endTime());

            //this.endDate();
            //this.endTime();

        }.bind(this);

        this.set3days = function() {

            //this.startDate(dateBySubtract(3, days));
            this.startDate(dateBySubtract2(this.endDate(), 3, days));
            //this.startTime(now());
            this.startTime(dateStartOf(day));

            //this.endDate(now());
            //this.endTime(now());
        }.bind(this);

        this.setToday = function() {

            this.startDate(now());
            this.startTime(dateStartOf(day));
            this.endDate(now());
            this.endTime(now());

        }.bind(this);

        this.getStartDate = function () {
            /*this.startDate(BWA.DataUtil.getDateTimeZeroMin(
                this.startDate(),
                this.startTime()));*/
            this.startTime(BWA.DataUtil.getDateTimeZeroMin(  //getDateTimeZeroSecond
                this.startDate(),
                this.startTime()));

            return BWA.DataUtil.getDateTimeFromDateAndTime(this.startDate(), this.startTime());
/*
            return BWA.DataUtil.getDateTimeZeroMin(
                this.startDate(),
                this.startTime());*/

        }.bind(this);

        this.getEndDate = function () {
/*
            this.endTime(BWA.DataUtil.getDateTimeZeroMin(
                this.endDate(),
                this.endTime()));

            return BWA.DataUtil.getDateTimeFromDateAndTime(
                this.endDate(),
                this.endTime()); */
            this.endTime(BWA.DataUtil.getDateTimeZeroMin(
                this.endDate(),
                this.endTime()));

            return BWA.DataUtil.getDateTimeFromDateAndTime(this.endDate(), this.endTime());


        }.bind(this);

        this.setToday();
    }

    function HourBox1() {
        this.startDate = ko.observable();
        this.startTime = ko.observable();
        this.endDate = ko.observable();
        this.endTime = ko.observable();
        /*
        this.startDate = new Date();
        this.startTime = new Date();
        this.endDate = new Date();
        this.endTime = new Date();*/

        this.timeChange = function () {
            alert('1');
        }.bind(this);

        this.set24hours = function () {
            //this.startDate(dateBySubtract(1, days));
            this.startDate(dateBySubtract2(this.endDate(), 1, days));
            this.startTime(this.endTime());

            //this.endDate();
            //this.endTime();

        }.bind(this);

        this.set3days = function () {

            //this.startDate(dateBySubtract(3, days));
            this.startDate(dateBySubtract2(this.endDate(), 3, days));
            //this.startTime(now());
            this.startTime(dateStartOf(day));

            //this.endDate(now());
            //this.endTime(now());
        }.bind(this);

        this.setToday = function () {

            this.startDate(now());
            this.startTime(dateStartOf(day));
            this.endDate(now());
            this.endTime(now());

        }.bind(this);

        this.getStartDate = function () {
            /*this.startDate(BWA.DataUtil.getDateTimeZeroMin(
                this.startDate(),
                this.startTime()));*/
            this.startTime(BWA.DataUtil.getDateTimeZeroSecond(  //getDateTimeZeroSecond
                this.startDate(),
                this.startTime()));

            return BWA.DataUtil.getDateTimeFromDateAndTime(this.startDate(), this.startTime());
            /*
                        return BWA.DataUtil.getDateTimeZeroMin(
                            this.startDate(),
                            this.startTime());*/

        }.bind(this);

        this.getEndDate = function () {
            /*
                        this.endTime(BWA.DataUtil.getDateTimeZeroMin(
                            this.endDate(),
                            this.endTime()));
            
                        return BWA.DataUtil.getDateTimeFromDateAndTime(
                            this.endDate(),
                            this.endTime()); */
            this.endTime(BWA.DataUtil.getDateTimeZeroSecond(
                this.endDate(),
                this.endTime()));

            return BWA.DataUtil.getDateTimeFromDateAndTime(this.endDate(), this.endTime());


        }.bind(this);

        this.setToday();
    }

    function DayBox() {

        this.startDate = ko.observable();
        this.endDate = ko.observable();

        this.set3days = function() {
            //this.startDate(dateBySubtract(3, days));
            this.startDate(dateBySubtract2(this.endDate(), 3, days));
            this.endDate(now());
        }.bind(this);

        this.set1week = function() {

            //this.startDate(dateBySubtract(7, days));
            this.startDate(dateBySubtract2(this.endDate(), 7, days));
            //this.endDate(now());
        }.bind(this);

        this.set1month = function() {

            //this.startDate(dateBySubtract(1, months));
            this.startDate(dateBySubtract2(this.endDate(), 1, months));
            //this.endDate(now());
        }.bind(this);

        this.setThisMonth = function() {
            this.startDate(dateStartOf(month));
            //this.startTime(dateStartOf(day)); // hcLee
            this.endDate(now());

        }.bind(this);

        this.getStartDate = function () {
            this.startDate(BWA.DataUtil.getDateTimeZeroHour(this.startDate()));
            return this.startDate();
            //return BWA.DataUtil.getDateTimeZeroHour(this.startDate());
        }.bind(this);

        this.getEndDate = function () {

            this.endDate(BWA.DataUtil.getDateTimeMaxHour(this.endDate()));
            return this.endDate();

        }.bind(this);

        this.setThisMonth();
    }

    function MonthBox(startthisyear) {

        this.startDate = ko.observable();
        this.endDate = ko.observable();

        this.set3months = function() {

            //this.startDate(dateBySubtract(2, months));
            this.startDate(dateBySubtract2(this.endDate(), 2, months));
            //this.endDate(now());
        }.bind(this);

        this.set6months = function() {

            //this.startDate(dateBySubtract(5, months));
            this.startDate(dateBySubtract2(this.endDate(), 5, months));
            //this.endDate(now());
        }.bind(this);

        this.set1year = function() {

            //this.startDate(dateBySubtract(11, months));
            this.startDate(dateBySubtract2(this.endDate(), 11, months));
            this.endDate(now());
        }.bind(this);

        this.setThisYear = function() {

            this.startDate(dateStartOf(year));
            this.endDate(now());

        }.bind(this);

        this.getStartDate = function() {
            //return this.startDate();

            this.startDate(BWA.DataUtil.getDateTimeStartDay(this.startDate()));
            return this.startDate();

        }.bind(this);

        this.getEndDate = function () {
            this.endDate(BWA.DataUtil.getDateTimeEndDay(this.endDate()));
            return this.endDate();

        }.bind(this);

        
        if (startthisyear == null)
            this.set1year(); // hcLee 2016 02 26 월별 조회가 기본인 화면들을 위하여 스타트 검색조건을 최근12개월로 변경한다.
        else
            this.setThisYear(); // // hcLee 2016 04 26 가동시간 비용 속도문제때문에
    }

    function YearBox() {

        this.startYear = ko.observable();
        this.endYear = ko.observable();

        this.set3years = function() {

            this.startYear(dateBySubtract(3, years).getFullYear());
            this.endYear(now().getFullYear());
        }.bind(this);

        this.set5years = function() {

            this.startYear(dateBySubtract(5, years).getFullYear());
            this.endYear(now().getFullYear());
        }.bind(this);

        this.set10years = function() {

            this.startYear(dateBySubtract(10, years).getFullYear());
            this.endYear(now().getFullYear());
        }.bind(this);

        this.getStartDate = function() {

            var year = this.startYear();
            return moment([year, 1, 1]).toDate();

        }.bind(this);

        this.getEndDate = function() {

            var year = this.startEnd();
            return moment([year, 1, 1]).toDate();

        }.bind(this);


        this.set3years();
    }

    var HOUR = $Code.TimeType.HOUR;
    var DAY = $Code.TimeType.DAY;
    var MONTH = $Code.TimeType.MONTH;
    var YEAR = $Code.TimeType.YEAR;

    // cyim 2016.08.22 : 생성자에 읽기전용인지 구분자 포함
    searchView.createDateTimeBox = function(dataSource, startthisyear, readonly) {

        var type = ko.observable();
        var isHour = ko.observable();
        var isDay = ko.observable();
        var isMonth = ko.observable();
        var isYear = ko.observable();
        var isReadOnly = ko.observable(false); // cyim 2016.08.22 : 성능분석 그래프의 경우 시간 데이타만 표시되어야 한다

        type.subscribe(function(type) {
            isHour(type === HOUR);
            isDay(type === DAY);
            isMonth(type === MONTH);
            isYear(type === YEAR);
        });

        // type(HOUR); 원래코드 

        // hcLee 2015 06 25 , 요일별 에너지 사용량분석 처럼 검색조건에 '시' 가 필요없는 경우를 처리 하기 위하여
        if (dataSource == null)
            type(HOUR);
        else type(dataSource[0].TimeTypeId);

        var dateTimeBox = {
            type: type,
            typeDataSource: (dataSource == null) ? $G.TimeTypes : dataSource, // hcLee 2015 06 25
            //typeDataSource: $G.TimeTypes, // // hcLee 2015 06 25
            isHour: isHour,
            isDay: isDay,
            isMonth: isMonth,
            isYear: isYear,
            isReadOnly: readonly, // cyim 2016.08.22 : 성능분석 그래프의 경우 시간 데이타만 표시되어야 한다

            hour: new HourBox(),
            day: new DayBox(),
            month: new MonthBox(startthisyear),
            year: new YearBox(),
        };

        //2016 07 21 hcLee
        dateTimeBox.setDefaultDate = function () {
            type(HOUR);
            this.hour.endTime(now());
            this.hour.set24hours();
        },

        dateTimeBox.getDate = function() {

            var timeObject = null;
            switch (this.type()) {
                case HOUR:
                    timeObject = this.hour;
                    break;
                case DAY:
                    timeObject = this.day;
                    break;
                case MONTH:
                    timeObject = this.month;
                    break;
                case YEAR:
                    timeObject = this.year;
                    break;
            }
            
            return {
                startDate: timeObject.getStartDate(),
                endDate: timeObject.getEndDate()
            };
        }.bind(dateTimeBox);

        return dateTimeBox;
    }

    searchView.createDateTimeBox1 = function (dataSource, startthisyear, readonly) {

        var type = ko.observable();
        var isHour = ko.observable();
        var isDay = ko.observable();
        var isMonth = ko.observable();
        var isYear = ko.observable();
        var isReadOnly = ko.observable(false); // cyim 2016.08.22 : 성능분석 그래프의 경우 시간 데이타만 표시되어야 한다

        type.subscribe(function (type) {
            isHour(type === HOUR);
            isDay(type === DAY);
            isMonth(type === MONTH);
            isYear(type === YEAR);
        });

        // type(HOUR); 원래코드 

        // hcLee 2015 06 25 , 요일별 에너지 사용량분석 처럼 검색조건에 '시' 가 필요없는 경우를 처리 하기 위하여
        if (dataSource == null)
            type(HOUR);
        else type(dataSource[0].TimeTypeId);

        var dateTimeBox = {
            type: type,
            typeDataSource: (dataSource == null) ? $G.TimeTypes : dataSource, // hcLee 2015 06 25
            //typeDataSource: $G.TimeTypes, // // hcLee 2015 06 25
            isHour: isHour,
            isDay: isDay,
            isMonth: isMonth,
            isYear: isYear,
            isReadOnly: readonly, // cyim 2016.08.22 : 성능분석 그래프의 경우 시간 데이타만 표시되어야 한다

            hour: new HourBox1(),
            day: new DayBox(),
            month: new MonthBox(startthisyear),
            year: new YearBox(),
        };

        //2016 07 21 hcLee
        dateTimeBox.setDefaultDate = function () {
            type(HOUR);
            this.hour.endTime(now());
            this.hour.set24hours();
        },

        dateTimeBox.getDate = function () {

            var timeObject = null;
            switch (this.type()) {
                case HOUR:
                    timeObject = this.hour;
                    break;
                case DAY:
                    timeObject = this.day;
                    break;
                case MONTH:
                    timeObject = this.month;
                    break;
                case YEAR:
                    timeObject = this.year;
                    break;
            }

            return {
                startDate: timeObject.getStartDate(),
                endDate: timeObject.getEndDate()
            };
        }.bind(dateTimeBox);

        return dateTimeBox;
    }
});