// Copyright 2024 Martin Riedl
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gomp4

import (
	"encoding/binary"
)

type Fixed88 struct {
	b []byte
}

func NewFixed88ByBytes(b []byte) Fixed88 {
	return Fixed88{
		b: b,
	}
}

func (fixed Fixed88) ToUInt16() uint16 {
	return uint16(fixed.b[0])
}

func (fixed Fixed88) ToFloat32() float32 {
	var result float32 = 0

	a := uint16(fixed.b[0])
	b := uint16(fixed.b[1])

	result += float32(a)
	result += float32(b) / 1_000 // max of uint8 is 255

	return result
}

type Fixed1616 struct {
	b []byte
}

func NewFixed1616ByBytes(b []byte) Fixed1616 {
	return Fixed1616{
		b: b,
	}
}

func (fixed Fixed1616) ToUInt16() uint16 {
	return binary.BigEndian.Uint16(fixed.b[0:2])
}

func (fixed Fixed1616) ToFloat32() float32 {
	var result float32 = 0

	a := binary.BigEndian.Uint16(fixed.b[0:2])
	b := binary.BigEndian.Uint16(fixed.b[2:4])

	result += float32(a)
	result += float32(b) / 65535 // max of uint16 is 65535

	return result
}
