package digitalocean

import (
	"context"
	"errors"
	"testing"

	"github.com/digitalocean/godo"
	"github.com/stretchr/testify/assert"
)

type mock struct {
	t *testing.T

	records_by_type map[string][]godo.DomainRecord

	edit_record_request *godo.DomainRecordEditRequest
	edit_record_error   error
}

func (m mock) List(context.Context, *godo.ListOptions) ([]godo.Domain, *godo.Response, error) {
	m.t.Error("List called when it should not have been")
	return nil, nil, nil
}

func (m mock) Get(context.Context, string) (*godo.Domain, *godo.Response, error) {
	m.t.Error("Get called when it should not have been")
	return nil, nil, nil
}

func (m mock) Create(context.Context, *godo.DomainCreateRequest) (*godo.Domain, *godo.Response, error) {
	m.t.Error("Create called when it should not have been")
	return nil, nil, nil
}

func (m mock) Delete(context.Context, string) (*godo.Response, error) {
	m.t.Error("Delete called when it should not have been")
	return nil, nil
}

func (m mock) Records(context.Context, string, *godo.ListOptions) ([]godo.DomainRecord, *godo.Response, error) {
	m.t.Error("Records called when it should not have been")
	return nil, nil, nil
}

func (m mock) RecordsByType(_ context.Context, name string, t string, opt *godo.ListOptions) ([]godo.DomainRecord, *godo.Response, error) {
	var err error

	// Only care about "A" records
	assert.Equal(m.t, "A", t)

	r, ok := m.records_by_type[name]
	if !ok {
		err = errors.New("Record not found")
	}
	return r, nil, err
}

func (m mock) RecordsByName(context.Context, string, string, *godo.ListOptions) ([]godo.DomainRecord, *godo.Response, error) {
	m.t.Error("RecordsByName called when it should not have been")
	return nil, nil, nil
}

func (m mock) RecordsByTypeAndName(context.Context, string, string, string, *godo.ListOptions) ([]godo.DomainRecord, *godo.Response, error) {
	m.t.Error("RecordsByTypeAndName called when it should not have been")
	return nil, nil, nil
}

func (m mock) Record(context.Context, string, int) (*godo.DomainRecord, *godo.Response, error) {
	m.t.Error("Record called when it should not have been")
	return nil, nil, nil
}

func (m mock) DeleteRecord(context.Context, string, int) (*godo.Response, error) {
	m.t.Error("DeleteRecord called when it should not have been")
	return nil, nil
}

func (m mock) EditRecord(_ context.Context, domain string, id int, req *godo.DomainRecordEditRequest) (*godo.DomainRecord, *godo.Response, error) {
	if m.edit_record_request == nil {
		m.t.Error("EditRecord called with empty request")
	}

	assert.Equal(m.t, m.edit_record_request, req)

	record := godo.DomainRecord{
		ID:       id,
		Type:     req.Type,
		Name:     req.Name,
		Data:     req.Data,
		Priority: req.Priority,
		Port:     req.Port,
		TTL:      req.TTL,
		Weight:   req.Weight,
		Flags:    req.Flags,
		Tag:      req.Tag,
	}

	return &record, nil, m.edit_record_error
}

func (m mock) CreateRecord(context.Context, string, *godo.DomainRecordEditRequest) (*godo.DomainRecord, *godo.Response, error) {
	m.t.Error("CreateRecord called when it should not have been")
	return nil, nil, nil
}
