WebSquare.uiplugin.gridView.prototype.defaultOptions.unformatOnPaste = false;

WebSquare.uiplugin.gridView.prototype.handlePasteEvent = function(e) {
    try {
        var preventPasteColumn = this.options.preventPasteColumn;
        if (((this.options.preventPaste || (preventPasteColumn && (preventPasteColumn.indexOf(this.getFocusedColumnID()) > -1))) && !this.editedCell) || (WebSquare.event.getTarget(e) && WebSquare.event.getTarget(e).getAttribute("class") && WebSquare.event.getTarget(e).getAttribute("class").indexOf("w2grid_head_input") > -1)) {
            return;
        }
        var valid = WebSquare.event.fireEvent(this, "oncustompaste", e);
        if (valid === false) {
            return;
        }
        var clipboardData = (WebSquare.util.isIEAllVersion()) ? window.clipboardData : e.clipboardData;
        var copiedData = clipboardData.getData("Text");

        if (copiedData.slice(-2) == "\r\n") {
            copiedData = copiedData.slice(0, copiedData.length - 2);
        } else if (copiedData.slice(-1) == "\n") {
            copiedData = copiedData.slice(0, copiedData.length - 1);
        }

        if (this.options.customPasteFormatter) {
            if (typeof this.options.customPasteFormatter === 'string') {
                var func = WebSquare.util.getGlobalFunction(this.options.customPasteFormatter.wq_trim(), this._scopeId);
                if (func) {
                    var returnData = func.call(this, copiedData);
                    copiedData = (returnData) ? returnData : copiedData;
                }
            } else {
                $l('customPasteFormatter의 type은 string이어야 합니다.');
            }
        }

        if (copiedData.slice(0, 1) === '"' && copiedData.slice(-1) === '"' && copiedData.slice(1, -1).indexOf("\t") == -1) {
            var rowData = [
                [copiedData.slice(1, copiedData.length - 1)]
            ];
        } else {
            var rowData = Papa.parse(copiedData, {
                delimiter: "\t"
            }).data;
        }
        if (!rowData || rowData == null || rowData == "undefined") {
            return;
        }
        if (this.focusedCell.length > 0) {
            var minRow = this.getDataLength();
            var maxRow = 0;
            var minCol = this.getTotalCol();
            var maxCol = 0;
            var startCell = null;
            var pasteInfo = {};

            for (var i = 0; i < this.focusedCell.length; i++) {
                var focusedCell = this.focusedCell[i];
                var rowIndex = focusedCell.focusedRowIndex;
                var colIndex = focusedCell.focusedColIndex;
                if (minRow > rowIndex) {
                    minRow = rowIndex;
                }
                if (maxRow < rowIndex) {
                    maxRow = rowIndex;
                }
                if (minCol > colIndex) {
                    minCol = colIndex;
                }
                if (maxCol < colIndex) {
                    maxCol = colIndex;
                }
            }

            var hiddenCols = 0;
            for (var i = this.defaultColCnt + minCol; i < maxCol; i++) {
                if (this.hiddenList[i] == true) {
                    hiddenCols += 1;
                }
            }

            if (this.focusedCell.length != (maxRow - minRow + 1) * (maxCol - minCol - hiddenCols + 1)) {
                return;
            } else {
                for (var i = 0; i < this.focusedCell.length; i++) {
                    focusedCell = this.focusedCell[i];
                    rowIndex = focusedCell.focusedRowIndex;
                    colIndex = focusedCell.focusedColIndex;
                    if (rowIndex == minRow && colIndex == minCol) {
                        startCell = focusedCell;
                        break;
                    }
                }
            }

            pasteInfo = {
                focusedRange: {
                    minRow: minRow,
                    maxRow: maxRow,
                    minCol: minCol,
                    maxCol: maxCol
                },
                startCell: startCell
            }

            if (startCell.focusedTd != null) {
                var srcElem = startCell.focusedTd;
                var tdId = srcElem.getAttribute("col_id");
                if (tdId == null || tdId.wq_trim() == "") {
                    return;
                }

                if (srcElem.getAttribute("colMerged") == "1") {
                    return;
                }
                var tdIndex = parseInt(srcElem.getAttribute("tdIndex"));
                var trIndex = parseInt(tdIndex / this.realRowDataLength);
                var rowIndex = trIndex + this.rowIndex;
                var colIndex = srcElem.getAttribute("col_id");
                var colIndexNum = this.getColumnIndex(colIndex);
                var cellInfo = this.getCellInfo(colIndex);
                var totalRow = this.getTotalRow();
                var totalCol = this.getTotalCol();

                if (this.options.checkBlankRowOnPaste) {
                    for (var i = rowData.length - 1; i > -1; i--) {
                        var blankRow = true;
                        for (var j = 0; j < rowData[0].length; j++) {
                            if (rowData[i][j].wq_trim() != "") {
                                blankRow = false;
                                break;
                            }
                        }
                        if (blankRow) {
                            rowData.splice(i, 1)
                        }
                    }
                }

                var ret = WebSquare.event.fireEvent(this, "onbeforepaste", {
                    "rowIndex": this._getGroupbyDisplayRowIndex(rowIndex),
                    "colIndex": colIndex,
                    "data": rowData.slice(0)
                });
                if (ret === false) {
                    return;
                }
                var _this = this;
                var findVisibleColIndex = function(colIndex) {
                    for (var i = _this.defaultColCnt + colIndex; i < _this.hiddenList.length; i++) {
                        if (_this.hiddenList[i] == false) {
                            return i - _this.defaultColCnt;
                        }
                    }
                }
                var comp_uuid = this.uuid;
                var pasteData = function(info) {
                    var comp = WebSquare.idCache[comp_uuid];
                    var checkReadOnlyOnPasteEnable = comp.options.checkReadOnlyOnPasteEnable;
                    if (rowIndex + rowData.length >= totalRow && !WebSquare.util.getBoolean(_this.options.preventAddRowOnPaste) && _this.options.pasteOption !== "focus") {

                        var rowDiff = (rowIndex + rowData.length) - totalRow;
                        var insertedDataArr = Array(_this._dataList.getTotalCol() * rowDiff);

                        if (_this.depthColumn != "") {
                            var depthColumnDefaultVal = _this._dataList.getCellInfo(_this.depthColumn).options.defaultValue;
                            for (var i = 0; i < rowDiff; i++) {
                                insertedDataArr[i * _this._dataList.getTotalCol() + _this._dataList.getColumnIndex(_this.depthColumn)] = depthColumnDefaultVal;
                            }
                        }
                        if (_this.options.setDefaultValueOnPaste) {
                            var dataListTotalCol = _this._dataList.getTotalCol();
                            for (var i = 0; i < dataListTotalCol; i++) {
                                var defaultVal = _this._dataList.getCellInfo(i).options.defaultValue;
                                for (var j = 0; j < rowDiff; j++) {
                                    insertedDataArr[j * dataListTotalCol + _this._dataList.getColumnIndex(i)] = defaultVal;
                                }
                            }
                        }

                        if (_this.options.checkReadOnlyOnPaste) {
                            var allColumnReadOnly = true;
                            var targetColIndex = colIndexNum;
                            for (var j = 0; j < rowData[0].length; j++) {
                                if (targetColIndex == undefined || targetColIndex >= totalCol) { 
                                    break;
                                }
                                if (!_this.___getEditReadOnly(rowIndex, targetColIndex)
                                    || (_this.___getEditReadOnly(rowIndex, targetColIndex) && checkReadOnlyOnPasteEnable && (checkReadOnlyOnPasteEnable.indexOf(comp.getColumnID(targetColIndex)) > -1))) {
                                    allColumnReadOnly = false;
                                    break;
                                }
                                targetColIndex = findVisibleColIndex(targetColIndex + 1);
                            }
                            if (allColumnReadOnly) { 
                                return;
                            }
                        }
                        if (_this.options.checkDisabledOnPaste) {
                            var allColumnDisabled = true;
                            var targetColIndex = colIndexNum;
                            for (var j = 0; j < rowData[0].length; j++) {
                                if (targetColIndex == undefined || targetColIndex >= totalCol) { 
                                    break;
                                }
                                if (!_this._getEditDisabled(rowIndex, targetColIndex)) {
                                    allColumnDisabled = false;
                                    break;
                                }
                            }
                            if (allColumnDisabled) { 
                                return;
                            }
                            targetColIndex = findVisibleColIndex(targetColIndex + 1);
                        }
                        if (insertedDataArr.length > 0) {
                            _this._dataList.setData(insertedDataArr, true);
                            for (var i = 0, value = "C", size = rowDiff, tempRowStatus = new Array(rowDiff); i < size; i++) {
                                tempRowStatus[i] = value;
                            }
                            _this._dataList.rowStatusArr = _this._dataList.rowStatusArr.slice(0, totalRow).concat(tempRowStatus);
                        }
                    }

                    for (var i = 0; i < rowData.length; i++) {
                        var targetColIndex = colIndexNum;
                        for (var j = 0; j < rowData[0].length; j++) {
                            if (targetColIndex == undefined || targetColIndex >= totalCol) { 
                                break;
                            }
                            if ((_this.options.checkReadOnlyOnPaste && _this.___getEditReadOnly(i + rowIndex, targetColIndex)) 
                                && !(checkReadOnlyOnPasteEnable && (checkReadOnlyOnPasteEnable.indexOf(comp.getColumnID(targetColIndex)) > -1))) {
                                targetColIndex = findVisibleColIndex(targetColIndex + 1);
                                continue;
                            }
                            if (_this.options.checkDisabledOnPaste && _this._getEditDisabled(i + rowIndex, targetColIndex)) { 
                                targetColIndex = findVisibleColIndex(targetColIndex + 1);
                                continue;
                            }
                            if (WebSquare.util.getBoolean(_this.options.preventAddRowOnPaste) && (_this.dataList.getRowCount() - 1 < i + rowIndex)) {
                                continue;
                            }
                            if (_this.options.pasteOption === "focus" && (targetColIndex > info.focusedRange.maxCol || targetColIndex < info.focusedRange.minCol
                                    || i + rowIndex > info.focusedRange.maxRow || i + rowIndex < info.focusedRange.minRow)) {
                                continue; 
                            }

                            var gridColIndex = targetColIndex;
                            var gridColId = _this.tdIdList[gridColIndex];
                            var dataColIndex = _this._dataList.getColumnIndex(gridColId);
                            var oldValue = _this._dataList.dataArr[(i + rowIndex) * _this._dataList.getColumnCount() + dataColIndex]; 
                            var newValue = rowData[i][j];
                            var gridCellInfo = _this.getCellInfo(gridColIndex);

                            if (gridCellInfo.options.inputType === 'select' && _this.options.selectByLabel) {
                                var itemArr = gridCellInfo.getItemArray();
                                var dataComp = gridCellInfo.mainGrid.modelControl.getDataComp(gridCellInfo.dataComp);
                                if (!itemArr && dataComp) {
                                    var labelColId = dataComp.bodyIdColIndexMap[gridCellInfo.itemsetObj.label];
                                    var valueColId = dataComp.bodyIdColIndexMap[gridCellInfo.itemsetObj.value];
                                    var oneRowLength = dataComp.cellIdList.length;
                                    var len = dataComp.filteredRowIndexArr.length;
                                    itemArr = [];
                                    for (var k = 0; k < len; k++) {
                                        var tmp = {};
                                        tmp.label = dataComp.dataArr[dataComp.filteredRowIndexArr[k] * oneRowLength + labelColId];
                                        tmp.value = dataComp.dataArr[dataComp.filteredRowIndexArr[k] * oneRowLength + valueColId];
                                        itemArr[k] = tmp;
                                    }
                                }
                                for (var l = 0; l < itemArr.length; l++) {
                                    var item = itemArr[l];
                                    if (item.label === newValue) {
                                        newValue = item.value;
                                        break;
                                    }
                                }
                            }
                            var oldSelectedIndex = gridCellInfo.comp && gridCellInfo.comp.selectedIndex;

                            if (_this.options.unformatOnPaste) { // [[bug:5826]] : [매일유업] copy&paste시 자릿수 제거 요청.
                                var retVal = newValue;
                                var masking = gridCellInfo.formatter.masking;
                                var valueType = gridCellInfo.formatter.type;
                                if (typeof valueType != 'undefined' && valueType != null && valueType == 'number') {
                                    retVal = WebSquare.text.unformatCurrency(masking, retVal);
                                } else if (typeof valueType != 'undefined' && valueType != null && valueType == 'date') {
                                    retVal = WebSquare.text.unformatDate(masking, retVal);
                                } else if (typeof valueType != 'undefined' && valueType != null && valueType == 'time') {
                                    retVal = WebSquare.text.unformatTime(masking, retVal);
                                } else {
                                    retVal = WebSquare.text.unformatText(masking, retVal);
                                }
                                newValue = retVal;
                            }

                            if (gridCellInfo.options.maxLength) {
                                var len = parseInt(gridCellInfo.options.maxLength);
                                if (!isNaN(len)) {
                                    newValue = newValue.slice(0, len);
                                }
                            }
                            if (gridCellInfo.options.allowChar) {
                                var gridColAllowChar = gridCellInfo.options.allowChar;
                                if (typeof gridColAllowChar != 'undefined' && gridColAllowChar != null && gridColAllowChar != "") {
                                    var re = new RegExp("[" + gridColAllowChar + "]");
                                    for (var k = 0; k < newValue.length; k++) {
                                        var s = newValue.charAt(k);
                                        if (s.match(re) == null) {
                                            return;
                                        }
                                    }
                                }
                            }
                            if (gridCellInfo.options.dataType == "number") {
                                newValue = newValue.wq_trim();
                            }
                            _this._dataList.setCellData(i + rowIndex, dataColIndex, newValue);
                            if (oldValue != newValue) {
                                var infoObj = {
                                    "oldValue": oldValue,
                                    "newValue": newValue,
                                    "rowIndex": (i + rowIndex),
                                    "colIndex": gridColIndex
                                };
                                if (!isNaN(oldSelectedIndex)) {
                                    infoObj.oldSelectedIndex = oldSelectedIndex;
                                    infoObj.newSelectedIndex = gridCellInfo.comp && gridCellInfo.comp.selectedIndex;
                                }
                                WebSquare.event.fireEvent(_this, "onviewchange", infoObj);
                            }
                            targetColIndex = findVisibleColIndex(targetColIndex + 1);
                        }
                    }
                }

                if (this.options.showMessageOnPaste) {
                    var processMsg = WebSquare.core.getConfiguration("/WebSquare/" + this.options.pluginName + "/pasteMessage/@value");
                    WebSquare.layer.pasteMessageLoaded = false;
                    WebSquare.layer.showProcessMessage(processMsg, {
                        type: "pasteMsg"
                    });
                    var checkPasteMsgLoad = function() {
                        if (!WebSquare.layer.pasteMessageLoaded) {
                            WebSquare.util.setTimeout(checkPasteMsgLoad, {
                                delay: 500,
                                key: _this.id + "_pasteMessageLoaded"
                            });
                        } else {
                            WebSquare.util.clearTimeout(_this.id + "_pasteMessageLoaded");
                            pasteData();
                            WebSquare.layer.hideProcessMessage({
                                type: "pasteMsg"
                            });
                            WebSquare.event.fireEvent(_this, "onafterpaste");
                        }
                    }
                    checkPasteMsgLoad();
                } else {
                    pasteData(pasteInfo);
                    WebSquare.event.fireEvent(this, "onafterpaste");
                }
            }
        }
        WebSquare.event.preventDefault(e);
    } catch (e) {
        WebSquare.exception.printStackTrace(e, null, this);
    }
};