package vt10x

import (
	
	
	
)

// CSI (Control Sequence Introducer)
// ESC+[
type csiEscape struct {
	buf  []byte
	args []int
	mode byte
	priv bool
}

func ( *csiEscape) () {
	.buf = .buf[:0]
	.args = .args[:0]
	.mode = 0
	.priv = false
}

func ( *csiEscape) ( byte) bool {
	.buf = append(.buf, )
	if  >= 0x40 &&  <= 0x7E || len(.buf) >= 256 {
		.parse()
		return true
	}
	return false
}

func ( *csiEscape) () {
	.mode = .buf[len(.buf)-1]
	if len(.buf) == 1 {
		return
	}
	 := string(.buf)
	.args = .args[:0]
	if [0] == '?' {
		.priv = true
		 = [1:]
	}
	 = [:len()-1]
	 := strings.Split(, ";")
	for ,  := range  {
		,  := strconv.Atoi()
		if  != nil {
			//t.logf("invalid CSI arg '%s'\n", p)
			break
		}
		.args = append(.args, )
	}
}

func ( *csiEscape) (,  int) int {
	if  >= len(.args) ||  < 0 {
		return 
	}
	return .args[]
}

// maxarg takes the maximum of arg(i, def) and def
func ( *csiEscape) (,  int) int {
	return max(.arg(, ), )
}

func ( *State) () {
	 := &.csi
	switch .mode {
	default:
		goto 
	case '@': // ICH - insert <n> blank char
		.insertBlanks(.arg(0, 1))
	case 'A': // CUU - cursor <n> up
		.moveTo(.cur.X, .cur.Y-.maxarg(0, 1))
	case 'B', 'e': // CUD, VPR - cursor <n> down
		.moveTo(.cur.X, .cur.Y+.maxarg(0, 1))
	case 'c': // DA - device attributes
		if .arg(0, 0) == 0 {
			// TODO: write vt102 id
		}
	case 'C', 'a': // CUF, HPR - cursor <n> forward
		.moveTo(.cur.X+.maxarg(0, 1), .cur.Y)
	case 'D': // CUB - cursor <n> backward
		.moveTo(.cur.X-.maxarg(0, 1), .cur.Y)
	case 'E': // CNL - cursor <n> down and first col
		.moveTo(0, .cur.Y+.arg(0, 1))
	case 'F': // CPL - cursor <n> up and first col
		.moveTo(0, .cur.Y-.arg(0, 1))
	case 'g': // TBC - tabulation clear
		switch .arg(0, 0) {
		// clear current tab stop
		case 0:
			.tabs[.cur.X] = false
		// clear all tabs
		case 3:
			for  := range .tabs {
				.tabs[] = false
			}
		default:
			goto 
		}
	case 'G', '`': // CHA, HPA - Move to <col>
		.moveTo(.arg(0, 1)-1, .cur.Y)
	case 'H', 'f': // CUP, HVP - move to <row> <col>
		.moveAbsTo(.arg(1, 1)-1, .arg(0, 1)-1)
	case 'I': // CHT - cursor forward tabulation <n> tab stops
		 := .arg(0, 1)
		for  := 0;  < ; ++ {
			.putTab(true)
		}
	case 'J': // ED - clear screen
		// TODO: sel.ob.x = -1
		switch .arg(0, 0) {
		case 0: // below
			.clear(.cur.X, .cur.Y, .cols-1, .cur.Y)
			if .cur.Y < .rows-1 {
				.clear(0, .cur.Y+1, .cols-1, .rows-1)
			}
		case 1: // above
			if .cur.Y > 1 {
				.clear(0, 0, .cols-1, .cur.Y-1)
			}
			.clear(0, .cur.Y, .cur.X, .cur.Y)
		case 2: // all
			.clear(0, 0, .cols-1, .rows-1)
		default:
			goto 
		}
	case 'K': // EL - clear line
		switch .arg(0, 0) {
		case 0: // right
			.clear(.cur.X, .cur.Y, .cols-1, .cur.Y)
		case 1: // left
			.clear(0, .cur.Y, .cur.X, .cur.Y)
		case 2: // all
			.clear(0, .cur.Y, .cols-1, .cur.Y)
		}
	case 'S': // SU - scroll <n> lines up
		.scrollUp(.top, .arg(0, 1))
	case 'T': // SD - scroll <n> lines down
		.scrollDown(.top, .arg(0, 1))
	case 'L': // IL - insert <n> blank lines
		.insertBlankLines(.arg(0, 1))
	case 'l': // RM - reset mode
		.setMode(.priv, false, .args)
	case 'M': // DL - delete <n> lines
		.deleteLines(.arg(0, 1))
	case 'X': // ECH - erase <n> chars
		.clear(.cur.X, .cur.Y, .cur.X+.arg(0, 1)-1, .cur.Y)
	case 'P': // DCH - delete <n> chars
		.deleteChars(.arg(0, 1))
	case 'Z': // CBT - cursor backward tabulation <n> tab stops
		 := .arg(0, 1)
		for  := 0;  < ; ++ {
			.putTab(false)
		}
	case 'd': // VPA - move to <row>
		.moveAbsTo(.cur.X, .arg(0, 1)-1)
	case 'h': // SM - set terminal mode
		.setMode(.priv, true, .args)
	case 'm': // SGR - terminal attribute (color)
		.setAttr(.args)
	case 'n':
		switch .arg(0, 0) {
		case 5: // DSR - device status report
			.w.Write([]byte("\033[0n"))
		case 6: // CPR - cursor position report
			.w.Write([]byte(fmt.Sprintf("\033[%d;%dR", .cur.Y+1, .cur.X+1)))
		}
	case 'r': // DECSTBM - set scrolling region
		if .priv {
			goto 
		} else {
			.setScroll(.arg(0, 1)-1, .arg(1, .rows)-1)
			.moveAbsTo(0, 0)
		}
	case 's': // DECSC - save cursor position (ANSI.SYS)
		.saveCursor()
	case 'u': // DECRC - restore cursor position (ANSI.SYS)
		.restoreCursor()
	}
	return
: // TODO: get rid of this goto
	.logf("unknown CSI sequence '%c'\n", .mode)
	// TODO: c.dump()
}