-- | https://datatracker.ietf.org/doc/html/rfc3284

module Haskpatch.Format.Vcdiff where

import Binrep.Type.Magic
import Binrep.Type.Varint
import Binrep.Type.Common
import Strongweak

import Numeric.Natural
import Data.ByteString ( ByteString )
import Data.Word ( Word8 )

data Vcdiff (s :: Strength) = Vcdiff
  { forall (s :: Strength). Vcdiff s -> Header s
vcdiffHeader :: Header s
  }

data Header (s :: Strength) = Header
  { forall (s :: Strength).
Header s -> SW s (Magic '[214, 195, 196, 0])
headerMagic :: SW s (Magic '[0xD6, 0xC3, 0xC4, 0x00])
  -- ^ First 3 bytes are @VCD@ each with their MSB on.

  , forall (s :: Strength). Header s -> SW s (Magic '[0])
headerIndicator :: SW s (Magic '[0x00])
  -- ^ TODO annoying and impacts rest of format. forcing to 0x00 to simplify
  }

data Window (s :: Strength) = Window
  { forall (s :: Strength). Window s -> SW s (Magic '[0])
windowIndicator :: SW s (Magic '[0x00]) -- TODO
  , forall (s :: Strength). Window s -> Delta s
windowDelta :: Delta s
  }

data Delta (s :: Strength) = Delta
  { forall (s :: Strength). Delta s -> SW s (Magic '[0])
deltaIndicator :: SW s (Magic '[0x00]) -- TODO compression indicators. ignoring
  , forall (s :: Strength). Delta s -> ByteString
deltaAddRun :: ByteString
  , forall (s :: Strength). Delta s -> [InstrCode]
deltaInstrs :: [InstrCode]
  , forall (s :: Strength). Delta s -> ByteString
deltaCopy   :: ByteString
  }

data InstrCode = InstrCode
  { InstrCode -> InstrTriple
instrCodeTriple1 :: InstrTriple
  , InstrCode -> InstrTriple
instrCodeTriple2 :: InstrTriple
  }

data InstrTriple = InstrTriple
  { InstrTriple -> Instr
instrTripleInstr :: Instr

  , InstrTriple -> Word8
instrTripleSize  :: Word8

  , InstrTriple -> Word8
instrTripleMode  :: Word8
  -- ^ 0 and meaningless unless instr is a COPY
  }

-- TODO singletons it
data Instr = Instr0Noop | Instr1Add | Instr2Run | Instr3Copy

-- | Apparently from the Sfio library, also similar (but not identical) to BPS's
--   varints.
type VcdiffVarint = Varnat 'Redundant 'OnContinues 'BE Natural