package modem

import (
	"fmt"
	"time"

	gpio "github.com/warthog618/go-gpiocdev"
)

func (m *modem) PowerOn() error {
	c, err := gpio.NewChip(m.deviceName)
	if err != nil {
		return fmt.Errorf("gpio new chip: %w", err)
	}
	l, err := c.RequestLine(m.powerKey, gpio.AsOutput(0))
	if err != nil {
		return fmt.Errorf("gpio request line: %w", err)
	}
	m.gpioLine = l
	time.Sleep(100 * time.Millisecond)
	if err := m.gpioLine.SetValue(1); err != nil {
		return fmt.Errorf("gpio set value: %w", err)
	}
	time.Sleep(3 * time.Second)
	if err := m.gpioLine.SetValue(0); err != nil {
		return fmt.Errorf("gpio set value: %w", err)
	}
	time.Sleep(30 * time.Second)
	return nil
}

func (m *modem) PowerOff() error {
	time.Sleep(100 * time.Millisecond)
	if err := m.gpioLine.SetValue(1); err != nil {
		return fmt.Errorf("gpio set value: %w", err)
	}
	time.Sleep(3 * time.Second)
	if err := m.gpioLine.SetValue(0); err != nil {
		return fmt.Errorf("gpio set value: %w", err)
	}
	time.Sleep(30 * time.Second)
	m.gpioLine = nil
	return nil
}
