// MIT License
//
// Copyright (c) 2020 Alex Howarth
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package tilt

import (
	"errors"
	"testing"
)

func TestNewTilt(t *testing.T) {
	tt := []struct {
		name       string
		ibeacon    *IBeacon
		color      Color
		celsius    float64
		fahrenheit uint16
		gravity    float64
		err        error
	}{
		{
			name:       "Red iBeacon",
			ibeacon:    &IBeacon{UUID: "a495bb10c5b14b44b5121370f02d74de", Major: 70, Minor: 1035},
			color:      Color("Red"),
			fahrenheit: 70,
			celsius:    21.11,
			gravity:    1.035,
			err:        nil,
		},
		{
			name:       "Black iBeacon",
			ibeacon:    &IBeacon{UUID: "a495bb30c5b14b44b5121370f02d74de", Major: 69, Minor: 1065},
			color:      Color("Black"),
			fahrenheit: 69,
			celsius:    20.56,
			gravity:    1.065,
			err:        nil,
		},
		{
			name:    "Not an iBeacon",
			ibeacon: &IBeacon{UUID: "a495bb99c5b14b44b5121370f02d74de", Major: 1, Minor: 2},
			err:     ErrNotTilt,
		},
	}

	for _, tc := range tt {

		t.Run(tc.name, func(t *testing.T) {
			got, err := NewTilt(tc.ibeacon)

			if tc.err != nil {
				// expecting an error
				if !errors.Is(err, tc.err) {
					t.Fatalf("Expected '%v' error, got '%v' error", tc.err, err)
				}
				return
			}

			if got.Color() != tc.color {
				t.Errorf("Expected %v, got %v", tc.color, got.Color())
			}
			if got.Celsius() != tc.celsius {
				t.Errorf("Expected %v, got %v", tc.celsius, got.Celsius())
			}
			if got.Fahrenheit() != tc.fahrenheit {
				t.Errorf("Expected %v, got %v", tc.fahrenheit, got.Fahrenheit())
			}
			if got.Gravity() != tc.gravity {
				t.Errorf("Expected %v, got %v", tc.gravity, got.Gravity())
			}
		})
	}
}
