require 'site'
local Nylon = require 'nylon.core'()


local Store = require 'pls-buffer'



   local function cursorpos( buffer, ptTopLeft, thePoint, wdim )
      local charsInto = thePoint - ptTopLeft
      local ptRow, ptCol
      local drow = 0
      local dcol = 0
      local charsDrawn = 0
--      local nextiseol
      for l, col, eol in buffer:walkFragmentsEOL( ptTopLeft ) do
         local toDraw = eol and (eol - col) or #l
--         if nextiseol then drow = drow + 1; nextiseol = false end
         if ((charsDrawn + toDraw) >= charsInto) then
            return (dcol + charsInto-charsDrawn), drow
         else
            charsDrawn = charsDrawn + toDraw
         end
         if eol then
            charsDrawn = charsDrawn + 1
            dcol = 0
            drow = drow + 1
--            nextiseol = true
         else
            dcol = dcol + toDraw
         end
      end
   end

local function mysert_eq( v, shouldbe )
   if v ~= shouldbe then
      print(string.format('expected value=%d got=%d',shouldbe,v))
      assert(false)
   end
end




local b = Store.withText('12345678a\n')
b:append('12345678b\n')
b:append('12345678c\n')

-- local n,c = b:lcol4point(21)

mysert_eq(b:end_point(),30)
b:insert(11,'12345678x\n')
mysert_eq(b:end_point(),40)

-- os.exit()


assert(b:char_at_point(3)=='3')
assert(b:char_at_point(14)=='4')
assert(b:char_at_point(25)=='5')
assert(b:char_at_point(36)=='6')


-- for l, col, eol in b:walkFragmentsEOL( 1 ) do
--    print( 'walk', #l, col, eol, l )
-- end

local function test_buffer( t, buflen)
   local b = Store.withText()
   b.setIdealStringLength(buflen)
   b:insert(1,t)
   local re = {}
   local rcvd = {}
   for l, col, eol in b:walkFragmentsEOL(1) do
      table.insert(rcvd, {col,l,eol} )
      table.insert(re, l:sub(col,eol))
   end

   local JSON = require 'JSON'

   if true or ( table.concat(re) ~= t ) then
      for i, v in ipairs(re) do
         print(i,JSON:encode(v))
         print(i,JSON:encode(rcvd[i]) )
      end
   end
   -- b:dump()
   assert(table.concat(re)==t)
   return b
end

if true then
   local t = 'hello\nthere\nbuddy\nroo\n' 
   test_buffer( t, 99 )
   test_buffer( t, 10 )

   local t = '0123456789'
   test_buffer( t, 5 )
   test_buffer( t, 3 )
   test_buffer( t, 2 )
   test_buffer( t, 1 )
end

--local b = 
local t = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'
test_buffer( t, 100 )
test_buffer( t, 10 )
test_buffer( t, 2 )
test_buffer( t, 1 )
--print 'adding 4'
--b:insert(5,'4')
--b:dump()
-- os.exit(0)

--- fail
local t = '0123456789'
test_buffer( t, 2 )

-- print( 'walk a01', #l, col, eol, l ) end

--for i = 1,40 do
--   local cx,cy = cursorpos(b,1,i)
--   print('cx=',cx,'cy=',cy)
----   assert(n==math.floor((i-1)/10)+1)
----   assert(c==((i-1)%10)+1)
--end


-- local b2 = Store.withText('12345678a\n12345678x\n12345678b\n12345678c\n')
-- for l, col, eol in b2:walkFragmentsEOL( 3 ) do
--    print( 'walk b2', l, col, eol )
-- end