import React,{Component} from 'react'
import {Input} from 'antd'



export default class ZTextArea extends Component
{

    shouldComponentUpdate(nextProps, nextState)
    {
        if((!this.props.value && !nextProps.value) || this.props.value == nextProps.value)
            return false
        return true
    }
    onChange=(e)=>
    {
        
        const rows = this.props.maxLength/this.props.cols
        let nval = e.target.value
        let oldval = this.props.value
        if((!oldval && ! nval) || nval== oldval)
            return
        replace2(this.eibs.refs.input,e.nativeEvent,rows,this.props.cols,this.props.value)
        
        this.props.onChange && this.props.onChange(e)
    }
    render()
    {
        const rows = this.props.maxLength/this.props.cols
        return <Input ref={ref=>this.eibs=ref} {...this.props} onChange={this.onChange} 
        maxLength={this.props.maxLength+rows}
        
         />
    }
}
function keyCode(evt) {
	return  evt.keyCode || evt.charCode;
}

function replace(obj,evt,func)
{
    if (evt.altKey)
		return;
	var k = keyCode(evt);
	if (evt.ctlKey && k != 'v' && k != 'x')
		return;
	if (k < 46 && k != 13 && k != 8)// BS=8,DEL=46,CR=13
		return;

	var result = obj.value;
    if(result)
    {
        result = result.replace(/\n/g,'')
        obj.value = result
    }     
	if (func)
		result = func(result);
	var cols = obj.cols;
	var rows = obj.rows;
	// var wr = document.selection.createRange(); // Store caret
	// obj.select();
	// var ar = document.selection.createRange();
	// wr.setEndPoint("StartToStart", ar);
	// var position = wr.text.replace(/\n/g, '').length;
    var position = getTxtCursorPosition(obj)
	if (k == 13 || (k == 8 && position % cols == 1))
		position++;
	// wr.collapse(false);
	// wr.select(); // End of store caret
	var s = obj.scrollTop;
	if (result && cols && rows && rows > 1) {
		var value = result;
		result = "";
		var line = "";
		for ( var i = 0; i < rows; i++) {
			var idx = value.indexOf('\r');
			if (idx >= 0) {
				line = value.substring(0, idx);
				value = value.substring(idx + 1);
			} else
				line = value;
			while (i < rows && line.length > cols) {
				result = result + (i == 0 ? "" : '\r')
						+ line.substring(0, cols);
				line = line.substring(cols);
				i++;
				position++;
			}
			if (i < rows)
				result = result + (i == 0 ? "" : '\r') + line;
			if (idx < 0)
				break;
		}
	}
	obj.value = result;
	// var tr = obj.createTextRange();
	// tr.collapse(true);
	// tr.moveStart('character', position);
	// tr.s();
	obj.scrollTop = s;
	// evt.stop();
    setTxtCursorPosition(obj,position)
    
}

const SpChar = '卐'
const LineChar = '\r'
function replace2(obj,evt,rows,cols,oldval)
{
    let result = obj.value;
    if((!result && !oldval ) || result == oldval)
        return
    result = result.replace(/[\r\n]{1,2}/g,'\r')
    
    let position = getTxtCursorPosition(obj)
    result = result.substring(0,position) + SpChar+ result.substring(position) //插入特殊符号
    let arr = result.split(/\r/g)
    
    let buf = []
    for(let p =0;p<arr.length;p++)
    {
        let item = arr[p]
        if(item.length <= cols || (item.length==cols+1 && item.charAt(cols)==SpChar))
        {
            buf.push(item)
            if(buf.length >= rows)
                break;
            continue
        }
 
        if(arr.length >= rows) //原本行数已经到达数量,那么当前行不应超标，当前字符不允许被插入，直接返回
        {
            obj.value = oldval
            setTxtCursorPosition(obj,position-1)
            return 
        }
        let temp = ''
        let cnt = 0
        for(let i=0;i<item.length;i++)
        {
           let c = item.charAt(i)
           if(c != SpChar)
                cnt++
           temp = temp + c
           if(cnt == cols)
           {
                buf.push(temp)
                temp = '' //标记清零
                cnt = 0
                if(buf.length >= rows)
                    break;
           }
           else if(i==item.length - 1) //最后一个字符，此段的最后一行
           {
                if(cnt==0 && oldval.length > result.length - 1) //删除下的末尾光标字符不应新增
                {

                }
                else
                    buf.push(temp)
           }
        }
        
        if(buf.length >= rows)
            break;
    }
    let msg = buf.join(LineChar)
    position = msg.indexOf(SpChar)
    if(position >= 0  )
    {
        msg = msg.substring(0,position) + msg.substring(position+1)
        // if(position < msg.length - 1)
        // position--
    }
    else
        position = msg.length
    obj.value=msg
    setTxtCursorPosition(obj,position)
    
}


 function setTxtCursorPosition(txtObj, pos) {

                var tempObj = txtObj;
                var cursurPosition = -1;

                if (tempObj.selectionStart != undefined) { //非IE浏览器
                    tempObj.setSelectionRange(pos, pos);
                } else { //IE
                    var range = tempObj.createTextRange();
                    range.move("character", pos);
                    range.select();
                }
}
function getTxtCursorPosition(txtObj) {

                var tempObj = txtObj;
                var cursurPosition = -1;

                if (tempObj.selectionStart != undefined) { //非IE浏览器
                    cursurPosition = tempObj.selectionStart;
                } else { //IE
                    var range = document.selection.createRange();
                    range.moveStart("character", -tempObj.value.length);
                    cursurPosition = range.text.length;
                }

                return cursurPosition;
}