package linkedservices

import (
	"encoding/json"
	"fmt"
)

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See NOTICE.txt in the project root for license information.

type ShopifyLinkedServiceTypeProperties struct {
	AccessToken           SecretBase `json:"accessToken"`
	EncryptedCredential   *string    `json:"encryptedCredential,omitempty"`
	Host                  string     `json:"host"`
	UseEncryptedEndpoints *bool      `json:"useEncryptedEndpoints,omitempty"`
	UseHostVerification   *bool      `json:"useHostVerification,omitempty"`
	UsePeerVerification   *bool      `json:"usePeerVerification,omitempty"`
}

var _ json.Unmarshaler = &ShopifyLinkedServiceTypeProperties{}

func (s *ShopifyLinkedServiceTypeProperties) UnmarshalJSON(bytes []byte) error {
	var decoded struct {
		EncryptedCredential   *string `json:"encryptedCredential,omitempty"`
		Host                  string  `json:"host"`
		UseEncryptedEndpoints *bool   `json:"useEncryptedEndpoints,omitempty"`
		UseHostVerification   *bool   `json:"useHostVerification,omitempty"`
		UsePeerVerification   *bool   `json:"usePeerVerification,omitempty"`
	}
	if err := json.Unmarshal(bytes, &decoded); err != nil {
		return fmt.Errorf("unmarshaling: %+v", err)
	}

	s.EncryptedCredential = decoded.EncryptedCredential
	s.Host = decoded.Host
	s.UseEncryptedEndpoints = decoded.UseEncryptedEndpoints
	s.UseHostVerification = decoded.UseHostVerification
	s.UsePeerVerification = decoded.UsePeerVerification

	var temp map[string]json.RawMessage
	if err := json.Unmarshal(bytes, &temp); err != nil {
		return fmt.Errorf("unmarshaling ShopifyLinkedServiceTypeProperties into map[string]json.RawMessage: %+v", err)
	}

	if v, ok := temp["accessToken"]; ok {
		impl, err := UnmarshalSecretBaseImplementation(v)
		if err != nil {
			return fmt.Errorf("unmarshaling field 'AccessToken' for 'ShopifyLinkedServiceTypeProperties': %+v", err)
		}
		s.AccessToken = impl
	}

	return nil
}
