package proto

import (
  "os"
  "google.golang.org/protobuf/proto"
  // "git.cems.dev/cdricms/bdooc/parsing"
)

func SaveToFile(data []uint8, filename string) error {
  EmployeeList := &EmployeeList{}
  err := proto.Unmarshal(data, EmployeeList)
  if err != nil {
    return err
  }
  return EmployeeList.SaveToFile(filename)
}

func (el *EmployeeList) SaveToFile(filename string) error {
  data, err := proto.Marshal(el)
  if err != nil {
    return err
  }
  file, err := os.Create(filename)
  if err != nil {
    return err
  }
  defer file.Close()
  _, err = file.Write(data)
  if err != nil {
    return err
  }
  return nil
}

func LoadFromFile(filename string) (*EmployeeList, error) {
  file, err := os.Open(filename)
  if err != nil {
    return nil, err
  }
  defer file.Close()
  data, err := os.ReadFile(filename)
  if err != nil {
    return nil, err
  }
  el := &EmployeeList{}
  err = proto.Unmarshal(data, el)
  if err != nil {
    return nil, err
  }
  return el, nil
}
