// Copyright 2016 Open Networking Laboratory
//
// 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 main

import (
	"encoding/json"
	consul "github.com/hashicorp/consul/api"
	"net/url"
)

const (
	PREFIX = "cord/provisioner/"
)

type ConsulStorage struct {
	client *consul.Client
	kv     *consul.KV
}

func NewConsulStorage(spec string) (*ConsulStorage, error) {
	conn, err := url.Parse(spec)
	if err != nil {
		return nil, err
	}

	cfg := consul.Config{
		Address: conn.Host,
		Scheme:  "http",
	}

	log.Debugf("Consul config = %+v", cfg)

	client, err := consul.NewClient(&cfg)
	if err != nil {
		return nil, err
	}
	return &ConsulStorage{
		client: client,
		kv:     client.KV(),
	}, nil
}

func (s *ConsulStorage) Put(id string, update StatusMsg) error {
	data, err := json.Marshal(update)
	if err != nil {
		return err
	}
	_, err = s.kv.Put(&consul.KVPair{
		Key:   PREFIX + id,
		Value: data,
	}, nil)
	return err
}

func (s *ConsulStorage) Delete(id string) error {
	_, err := s.kv.Delete(PREFIX+id, nil)
	return err
}

func (s *ConsulStorage) Get(id string) (*StatusMsg, error) {
	pair, _, err := s.kv.Get(PREFIX+id, nil)
	if err != nil {
		return nil, err
	}

	if pair == nil {
		return nil, nil
	}

	var record StatusMsg
	err = json.Unmarshal([]byte(pair.Value), &record)
	if err != nil {
		return nil, err
	}
	return &record, nil
}

func (s *ConsulStorage) List() ([]StatusMsg, error) {
	pairs, _, err := s.kv.List(PREFIX, nil)
	if err != nil {
		return nil, err
	}
	result := make([]StatusMsg, len(pairs))
	i := 0
	for _, pair := range pairs {
		err = json.Unmarshal([]byte(pair.Value), &(result[i]))
		if err != nil {
			return nil, err
		}
		i += 1
	}
	return result, nil
}
