
import {RegMap} from './TagRegMap'

export function isTagValueEmpty(tag,tagValue)
{
    let obj = tagValue[0]
    let regobj = RegMap[tag]
    for(let key in regobj)
    {
        if(obj[key])
            return false
    }
    return true
}

export function TagV(tag,status,value,tno,mty,seqlist,countMap={})
{
	let regobj = RegMap[tag]
    let obj = value[0]
    let mval = value[1]
    
     //计算状态 status，是否是必填的，要参考所在序列是否是必填，上一层序列计数，来决定当前序列是否必填  
    if(seqlist && seqlist.length && status == 'M' && isTagValueEmpty(tag,value))
    {
         let curseq = seqlist[seqlist.length - 1]
         let curcnt = countMap[curseq.seqlist] || 0
         if(curseq.status == 'O' &&  curcnt==0)
         {
             status = 'O'
         }
         else if(curseq.status == 'M' && curcnt==0)
         {
             //验证上层是否有计数为O，且上层计数
             for(let i = seqlist.length - 2 ; i >=0;i--)
             {
                     let itemseq = seqlist[i]
                     let itemcnt = countMap[itemseq.seqlist] || 0
                     if(itemcnt>0)
                         break
                     if(itemseq.status == 'O' && itemcnt == 0)
                         status = 'O'
                     

             }
         }
    }
    
    let errmsg = null
    if(value.length == 3)
         errmsg = value[2] || {}
    else
    {
        errmsg = {}
        value.push(errmsg)
    }
    let isAllEmpty = true

    for(let key in regobj)
    {
        if(!regobj[key])
        {
            errmsg[key]=""
            continue
        }    
        let reg = regobj[key]
        let val = obj[key] 
        if(!val)
            obj[key]=undefined
        else
        {
             isAllEmpty = false
             //val = FormatAdr(val)

        }   

        if(reg.option=='O')
        {
            if(!val)
            {
                errmsg[key]=""
                if(!val && (tag=='T11R' || tag=='T11S') && (key=='s3' || key=='s4') )
                {
                    if(obj.s3||obj.s4)
                        errmsg[key]="Session Number 、ISN必须同时填写"
                    
                }
                continue
            }
            //校验值
            if(reg.line>1)
            {
                val = formatAdr(val,reg.line,reg.length)
                obj[key] = val
                let line_temp = reg.line
                if(mty== "mt700")   //特殊化处理，放开至800
                {
                    if(tag == 'T45A' || tag == 'T46A' || tag == 'T47A' ||tag == 'T49G' ||tag == 'T49H')
                        line_temp = 800
                }
                else if(mty=="mt707")
                {
                    if(tag == 'T45B' || tag == 'T46B' || tag == 'T47B' ||tag == 'T49M' ||tag == 'T49N')
                        line_temp = 800
                }
                errmsg[key]=TestAdr(val,reg.reg,line_temp,reg.length)
                continue
            }
            if(reg.content == 'AMOUNT' || reg.content == 'NUMBER' || reg.type=='d')
            {
                errmsg[key]=TestAmt(val,reg.length)
                continue
            }
            
            if(!new RegExp(`^${reg.reg}$`).test(val))
            {
                    errmsg[key]="请输入正确的格式"  
            }   
            continue
        }
        //必填
        if(!val)
        {
            if(status=='M') //栏位已经填了值，或者该栏位必填
                errmsg[key]="请填写必输项"
            else if(mval)
                errmsg[key]="请填写完整"
        }
        else
        {
            //校验值
            if(reg.line>1)
            {
                val = formatAdr(val,reg.line,reg.length)
                obj[key] = val
                let line_temp = reg.line
                if(mty== "mt700")   //特殊化处理，放开至800
                {
                    if(tag == 'T45A' || tag == 'T46A' || tag == 'T47A' ||tag == 'T49G' ||tag == 'T49H')
                        line_temp = 800
                }
                else if(mty=="mt707")
                {
                    if(tag == 'T45B' || tag == 'T46B' || tag == 'T47B' ||tag == 'T49M' ||tag == 'T49N')
                        line_temp = 800
                }
                errmsg[key]=TestAdr(val,reg.reg,line_temp,reg.length)
                continue
            }
            if(reg.content == 'AMOUNT' || reg.content == 'NUMBER' || reg.type=='d')
            {
                errmsg[key]=TestAmt(val,reg.length)
                continue
            }
            
            if(!new RegExp(`^${reg.reg}$`).test(val))
            {   
                errmsg[key]="请输入正确的格式"
            }   
        }
    }
    if(isAllEmpty)
    {
        value[1] = ''
        if(status == 'O'){
            for(let ek in errmsg)
            {
                
                    errmsg[ek] = '' 
            }
        }
    }
    for(let ek in errmsg)
    {
        if(errmsg[ek])
            return false
    }
    return true
}


function FormatAdr(val)
{
    val = val.replace(/[\\r\\n]{1,2}/g,'\r\n')
    let val_bak = ""
    let lineCount = 0
    let begIndex = 0
    let cur = 0

    while(cur < val.length)
    {

    }


    return val
}

function TestAmt(amt,length)
{
    if(!/^\d+(\.\d*)?$/.test(amt))
    {
        return "请填写正确的金额"
    }
    let idx = amt.indexOf('.')
    if(idx >= 0)
    {
         if(amt.length > length + 1)
            return "数字长度过长"
    }else  
     {
        if(amt.length > length)
            return "数字长度过长"
     }     
    return ""   
}
function  formatAdr(adr,line,length){
    return adr
}
function formatAdr2(adr,line,length)
{
    let arr = []
    let i =0
    let cols = 0
    let wordBegin = 0 
    let wordEnd = 0
    let lines = 0
    let lastLineEnd = -1
    adr = adr.replace(/[\r\n]{1,2}/g,'\n')
    while(i<adr.length)
    {
        let c = adr.charAt(i)
        if(c=='\n')
        {
            lines++
            if(lines >= line) //行数达到
                break;
            arr.push(c)
            cols = 0 //换行列数清零
            wordBegin = arr.length - 1
            lastLineEnd = wordBegin - 1
        }
        else if(!/[0-9A-Za-z]/.test(c))
        {
            wordBegin = -1 //没有捕获到单词
            arr.push(c)
            cols++
        }
        else{
            //其他字符
            if(wordBegin < 0)
                wordBegin = arr.length
            arr.push(c)
            cols++
        }

        if(cols == length+1)
        {
            lines++
            if(wordBegin - lastLineEnd  == 1)   //行开始的单词，超长单词
            {
                if(lines >=line)
                    break;
                arr.splice(arr.length-1,0,'\n')
                wordBegin=arr.length - 1
                lastLineEnd = wordBegin - 1
                cols = 0
            }
            else if(wordBegin > 0)
            {
                cols = arr.length - wordBegin //回溯部分长度
                if(lines >=line)
                {
                       arr.splice(wordBegin,arr.length-wordBegin) //最后一行了，直接不要了 
                       break;
                }
                 
                arr.splice(wordBegin,0,'\n') //插入一个换行符
                lastLineEnd = wordBegin
                wordBegin+=1
            } 
            else 
            {
                if(lines >=line)
                    break;
                arr.splice(arr.length-1,0,'\n')
                wordBegin=arr.length
                lastLineEnd = wordBegin - 1
                cols = 0
            }           
        }
        i++
    }
    return arr.join('')
    // return adr
}

function TestAdr(adr,reg,line,length)
{
    let arr = adr.split(/[\r\n]{1,2}/g)

    let a1 = []
    let a2 = []

    for(let i=0;i<arr.length;i++)
    {
        
        if(arr[i].length > length)
            a1.push(i+1)
        if(arr[i].length <= length && !new RegExp(`^${reg}$`).test(arr[i]))
            a2.push(i+1)
    }
    let linemsg = ''
    if(arr.length > line)
        linemsg = `最多${line}行`
    let msg = []
    if(a1.length)
    {
        msg.push( `第${a1.join(',')}行超过${length}个字符`)
    }
    if(a2.length)
        msg.push(`第${a2.join(',')}行格式非法`)
    if(linemsg)
        msg.push(linemsg)
    return msg.join(';')
}