package ciena

import (
	"bytes"
	"code.fbi.h-da.de/cocsn/gosdn/database"
	"code.fbi.h-da.de/cocsn/gosdn/log"
	"code.fbi.h-da.de/cocsn/gosdn/nucleus/interfaces"
	"code.fbi.h-da.de/cocsn/gosdn/sbi/restconf/util"
	apiclient "code.fbi.h-da.de/cocsn/swagger/apis/mcp/client"
	"code.fbi.h-da.de/cocsn/yang-modules/generated/tapi"
	"crypto/tls"
	"github.com/go-openapi/runtime"
	httptransport "github.com/go-openapi/runtime/client"
	"github.com/go-openapi/strfmt"
	"github.com/openconfig/ygot/ygot"
	"net/http"
)

//MCPClient handles requests to a Ciena MCP RESTCONF endpoint
type MCPClient struct {
	transport *httptransport.Runtime
	client    *apiclient.ServiceTopologyTAPI
	database  *database.Database
	buffer    *bytes.Buffer
	config    *interfaces.ClientConfig
	device    ygot.GoStruct
}

// GetConfig returns a ClientConfig struct containing
// the current configuration stat of the Ciena SBI client
func (c MCPClient) GetConfig() interfaces.ClientConfig {
	return *c.config
}

//NewMCPClient creates a Ciena flavores TAPI client
func NewMCPClient(endpoint, username, password string, database *database.Database, config *interfaces.ClientConfig) *MCPClient {
	// create the transport
	transport := httptransport.New(endpoint, "/", nil)
	transport.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
	// create the API client, with the transport
	basicAuth := httptransport.BasicAuth(username, password)
	// authenticate client
	transport.DefaultAuthentication = basicAuth
	client := apiclient.New(transport, strfmt.Default)

	buffer := new(bytes.Buffer)
	transport.Consumers[runtime.JSONMime] = util.YANGConsumer{Data: buffer}

	return &MCPClient{
		transport: transport,
		client:    client,
		database:  database,
		buffer:    buffer,
		config:    config,
		device:    &tapi.Device{},
	}
}

// GetConnections implements the TAPI Connectivity GetConnections call with a grain of
// Ciena salt. The response is written to the client's buffer and passed to the database
func (c *MCPClient) GetConnections() error {
	defer c.buffer.Reset()
	_, err := c.client.TapiConnectivityCore.GetTapiCoreContextConnection(nil)
	if err != nil { return err}
	dest := &tapi.TapiCommon_Context_ConnectivityContext_Connection{}
	if err := tapi.Unmarshal(c.buffer.Bytes(), dest); err != nil { return err }
	c.database.StoreConnections(c.buffer.String())
	log.Debug(c.buffer.Next(25))
	return err
}

// GetLinks implements the TAPI Topology GetLinks call with a grain of
// Ciena salt. The response is written to the client's buffer and passed to the database
func (c *MCPClient) GetLinks() error {
	defer c.buffer.Reset()
	_, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyLink(nil)
	if err != nil { return err}
	dest := &tapi.TapiCommon_Context_TopologyContext_Topology_Link{}
	if err := tapi.Unmarshal(c.buffer.Bytes(), dest); err != nil { return err }
	c.database.StoreLinks(c.buffer.String())
	log.Debug(c.buffer.Next(25))
	return err
}

// GetNodes implements the TAPI Topology GetNodes call with a grain of
// Ciena salt. The response is written to the client's buffer and passed to the database
func (c *MCPClient) GetNodes() error {
	defer c.buffer.Reset()
	_, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyNode(nil)
	if err != nil { return err}
	dest := &tapi.TapiCommon_Context_TopologyContext_Topology_Node{}
	if err := tapi.Unmarshal(c.buffer.Bytes(), dest); err != nil { return err }
	c.database.StoreNodes(c.buffer.String())
	log.Debug(c.buffer.Next(25))
	return err
}

// GetNodeEdgePoints implements the TAPI Topology GetNodeEdgePoints call with a grain of
// Ciena salt. The response is written to the client's buffer and passed to the database
func (c *MCPClient) GetNodeEdgePoints() error {
	defer c.buffer.Reset()
	_, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyNodeEdgePoint(nil)
	if err != nil { return err}
	dest := &tapi.TapiCommon_Context_TopologyContext_Topology_Link_NodeEdgePoint{}
	if err := tapi.Unmarshal(c.buffer.Bytes(), dest); err != nil { return err }
	c.database.StoreNodeEdgePoints(c.buffer.String())
	log.Debug(c.buffer.Next(25))
	return err
}