package rangeplugin

import (
	"encoding/csv"
	"fmt"
	"io"
	"os"
)

const staticIpFile string = "./static_ip.csv"

var staticIpPool map[string]string // mac as key
var macPool map[string]string // ip as key

func importStaticIpFile() {
	fs, err := os.Open(staticIpFile)
	if err != nil {
		//fmt.Println(err)
		return
	}
	defer fs.Close()

	staticIpPool = make(map[string]string)
	macPool = make(map[string]string)

	r := csv.NewReader(fs)
	for {
		row, err := r.Read()
		if err != nil && err != io.EOF {
			fmt.Printf("Can not read, err is %+v", err)
		}
		if err == io.EOF {
			break
		}
		//fmt.Println(row)
		if len(row) > 1 {
			staticIpPool[row[0]] = row[1]
			macPool[row[1]] = row[0]
		}
	}
}

func GetStaticIp(mac string) string {
	return staticIpPool[mac]
}

func IsStaticIp(ip string) bool {
	_, ok := macPool[ip]
	return ok
}