package uv

import (
	

	
)

// scrollOptimize optimizes the screen to transform the old buffer into the new
// buffer.
func ( *TerminalRenderer) ( *RenderBuffer) {
	 := .Height()
	if .oldnum == nil || len(.oldnum) <  {
		.oldnum = append(.oldnum, make([]int, -len(.oldnum))...)
	}

	// Calculate the indices
	.updateHashmap()
	if len(.hashtab) <  {
		return
	}

	// Pass 1 - from top to bottom scrolling up
	for  := 0;  < ; {
		for  <  && (.oldnum[] == newIndex || .oldnum[] <= ) {
			++
		}
		if  >=  {
			break
		}

		 := .oldnum[] -  // shift > 0
		 := 

		++
		for  <  && .oldnum[] != newIndex && .oldnum[]- ==  {
			++
		}
		 :=  - 1 + 

		if !.scrolln(, , , , -1) {
			continue
		}
	}

	// Pass 2 - from bottom to top scrolling down
	for  :=  - 1;  >= 0; {
		for  >= 0 && (.oldnum[] == newIndex || .oldnum[] >= ) {
			--
		}
		if  < 0 {
			break
		}

		 := .oldnum[] -  // shift < 0
		 := 

		--
		for  >= 0 && .oldnum[] != newIndex && .oldnum[]- ==  {
			--
		}

		 :=  + 1 - (-)
		if !.scrolln(, , , , -1) {
			continue
		}
	}
}

// scrolln scrolls the screen up by n lines.
func ( *TerminalRenderer) ( *RenderBuffer, , , ,  int) ( bool) { //nolint:unparam
	 := .clearBlank()
	if  > 0 { //nolint:nestif
		// Scroll up (forward)
		 = .scrollUp(, , , , 0, , )
		if ! {
			.buf.WriteString(ansi.SetTopBottomMargins(+1, +1))

			// XXX: How should we handle this in inline mode when not using alternate screen?
			.cur.X, .cur.Y = -1, -1
			 = .scrollUp(, , , , , , )
			.buf.WriteString(ansi.SetTopBottomMargins(1, +1))
			.cur.X, .cur.Y = -1, -1
		}

		if ! {
			 = .scrollIdl(, , , -+1, )
		}
	} else if  < 0 {
		// Scroll down (backward)
		 = .scrollDown(, -, , , 0, , )
		if ! {
			.buf.WriteString(ansi.SetTopBottomMargins(+1, +1))

			// XXX: How should we handle this in inline mode when not using alternate screen?
			.cur.X, .cur.Y = -1, -1
			 = .scrollDown(, -, , , , , )
			.buf.WriteString(ansi.SetTopBottomMargins(1, +1))
			.cur.X, .cur.Y = -1, -1

			if ! {
				 = .scrollIdl(, -, ++1, , )
			}
		}
	}

	if ! {
		return false
	}

	.scrollBuffer(.curbuf, , , , )

	// shift hash values too, they can be reused
	.scrollOldhash(, , )

	return true
}

// scrollBuffer scrolls the buffer by n lines.
func ( *TerminalRenderer) ( *RenderBuffer, , ,  int,  *Cell) {
	if  < 0 ||  <  ||  >= .Height() {
		// Nothing to scroll
		return
	}

	if  < 0 {
		// shift n lines downwards
		 :=  - 
		for  := ;  >=  &&  >= 0 &&  >= ; -- {
			copy(.Lines[], .Lines[+])
		}
		for  := ;  <  &&  <= .Height()-1 &&  <= ; ++ {
			.FillArea(, Rect(0, , .Width(), 1))
		}
	}

	if  > 0 {
		// shift n lines upwards
		 :=  - 
		for  := ;  <=  &&  <= .Height()-1 &&  <= ; ++ {
			copy(.Lines[], .Lines[+])
		}
		for  := ;  >  &&  >= 0 &&  >= ; -- {
			.FillArea(, Rect(0, , .Width(), 1))
		}
	}

	.touchLine(, , -+1, true)
}

// touchLine marks the line as touched.
func ( *TerminalRenderer) ( *RenderBuffer, ,  int,  bool) {
	 := .Height()
	if  < 0 ||  < 0 ||  >=  || .Touched == nil || len(.Touched) <  {
		return // Nothing to touch
	}

	 := .Width()
	for  := ;  < + &&  <  &&  < len(.Touched); ++ {
		if  {
			.TouchLine(0, , )
		} else {
			.Touched[] = nil
		}
	}
}

// scrollUp scrolls the screen up by n lines.
func ( *TerminalRenderer) ( *RenderBuffer, , , , ,  int,  *Cell) bool {
	if  == 1 &&  ==  &&  ==  { //nolint:nestif
		.move(, 0, )
		.updatePen()
		.buf.WriteByte('\n')
	} else if  == 1 &&  ==  {
		.move(, 0, )
		.updatePen()
		.buf.WriteString(ansi.DeleteLine(1))
	} else if  ==  &&  ==  {
		 := .caps.Contains(capSU)
		.move(, 0, )
		.updatePen()
		if  {
			.buf.WriteString(ansi.ScrollUp())
		} else {
			.buf.WriteString(strings.Repeat("\n", ))
		}
	} else if  ==  {
		.move(, 0, )
		.updatePen()
		.buf.WriteString(ansi.DeleteLine())
	} else {
		return false
	}
	return true
}

// scrollDown scrolls the screen down by n lines.
func ( *TerminalRenderer) ( *RenderBuffer, , , , ,  int,  *Cell) bool {
	if  == 1 &&  ==  &&  ==  { //nolint:nestif
		.move(, 0, )
		.updatePen()
		.buf.WriteString(ansi.ReverseIndex)
	} else if  == 1 &&  ==  {
		.move(, 0, )
		.updatePen()
		.buf.WriteString(ansi.InsertLine(1))
	} else if  ==  &&  ==  {
		.move(, 0, )
		.updatePen()
		if .caps.Contains(capSD) {
			.buf.WriteString(ansi.ScrollDown())
		} else {
			.buf.WriteString(strings.Repeat(ansi.ReverseIndex, ))
		}
	} else if  ==  {
		.move(, 0, )
		.updatePen()
		.buf.WriteString(ansi.InsertLine())
	} else {
		return false
	}
	return true
}

// scrollIdl scrolls the screen n lines by using [ansi.DL] at del and using
// [ansi.IL] at ins.
func ( *TerminalRenderer) ( *RenderBuffer, , ,  int,  *Cell) bool {
	if  < 0 {
		return false
	}

	// Delete lines
	.move(, 0, )
	.updatePen()
	.buf.WriteString(ansi.DeleteLine())

	// Insert lines
	.move(, 0, )
	.updatePen()
	.buf.WriteString(ansi.InsertLine())

	return true
}