/*
DESCRIPTION
  See package description.

AUTHORS
  Saxon A. Nelson-Milton <saxon@ausocean.org>

LICENSE
  Copyright (C) 2019 the Australian Ocean Lab (AusOcean)

  It is free software: you can redistribute it and/or modify them
  under the terms of the GNU General Public License as published by the
  Free Software Foundation, either version 3 of the License, or (at your
  option) any later version.

  It is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  for more details.

  You should have received a copy of the GNU General Public License
  in gpl.txt.  If not, see http://www.gnu.org/licenses.
*/

package main

import (
	"flag"
	"fmt"
	"strconv"

	"bitbucket.org/ausocean/av/device/geovision/config"
)

func main() {
	var (
		hostPtr       = flag.String("host", "192.168.1.50", "IP of GeoVision camera.")
		codecPtr      = flag.String("codec", "", "h264, h265 or mjpeg")
		heightPtr     = flag.Int("height", 0, "256, 360 or 720")
		fpsPtr        = flag.Int("fps", 0, "Frame rate in frames per second.")
		vbrPtr        = flag.Bool("vbr", false, "If true, variable bitrate.")
		vbrQualityPtr = flag.Int("quality", -1, "General quality under variable bitrate, 0 to 4 inclusive.")
		vbrRatePtr    = flag.Int("vbr-rate", 0, "Variable bitrate maximal bitrate in kbps.")
		cbrRatePtr    = flag.Int("cbr-rate", 0, "Constant bitrate, bitrate in kbps.")
		refreshPtr    = flag.Float64("refresh", 0, "Inter refresh period in seconds.")
	)
	flag.Parse()

	var options []config.Option

	if *codecPtr != "" {
		var c config.Codec
		switch *codecPtr {
		case "h264":
			c = config.CodecH264
		case "h265":
			c = config.CodecH265
		case "mjpeg":
			c = config.CodecMJPEG
		default:
			panic(fmt.Sprintf("invalid codec: %s", *codecPtr))
		}
		options = append(options, config.CodecOut(c))
	}

	if *heightPtr != 0 {
		options = append(options, config.Height(*heightPtr))
	}

	if *fpsPtr != 0 {
		options = append(options, config.FrameRate(*fpsPtr))
	}

	options = append(options, config.VariableBitrate(*vbrPtr))

	if *vbrQualityPtr != -1 {
		options = append(options, config.VBRQuality(config.Quality(strconv.Itoa(*vbrQualityPtr))))
	}

	if *vbrRatePtr != 0 {
		options = append(options, config.VBRBitrate(*vbrRatePtr))
	}

	if *cbrRatePtr != 0 {
		options = append(options, config.CBRBitrate(*cbrRatePtr))
	}

	if *refreshPtr != 0 {
		options = append(options, config.Refresh(*refreshPtr))
	}

	err := config.Set(*hostPtr, options...)
	if err != nil {
		panic(fmt.Sprintf("error from config.Set: %v", err))
	}
}