package roblox

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"time"
)

// Product contains info about a product
type Product struct {
	TargetID    int    `json:"TargetId"`
	ProductType string `json:"ProductType"`
	AssetID     int    `json:"AssetId"`
	ProductID   int    `json:"ProductId"`
	Name        string `json:"Name"`
	Description string `json:"Description"`
	AssetTypeID int    `json:"AssetTypeId"`
	Creator     struct {
		ID              int    `json:"Id"`
		Name            string `json:"Name"`
		CreatorType     string `json:"CreatorType"`
		CreatorTargetID int    `json:"CreatorTargetId"`
	} `json:"Creator"`
	IconImageAssetID       int       `json:"IconImageAssetId"`
	Created                time.Time `json:"Created"`
	Updated                time.Time `json:"Updated"`
	PriceInRobux           int       `json:"PriceInRobux"`
	PriceInTickets         int       `json:"PriceInTickets"`
	Sales                  int       `json:"Sales"`
	IsNew                  bool      `json:"IsNew"`
	IsForSale              bool      `json:"IsForSale"`
	IsPublicDomain         bool      `json:"IsPublicDomain"`
	IsLimited              bool      `json:"IsLimited"`
	IsLimitedUnique        bool      `json:"IsLimitedUnique"`
	Remaining              int       `json:"Remaining"`
	MinimumMembershipLevel int       `json:"MinimumMembershipLevel"`
	ContentRatingTypeID    int       `json:"ContentRatingTypeId"`
}

// GetProduct will retrieve store information on a product
func (s *Session) GetProduct(id int) (*Product, error) {
	resp, err := s.client.Get(fmt.Sprintf("http://api.roblox.com/marketplace/productinfo?assetId=%d", id))
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	var info Product
	err = json.Unmarshal(data, &info)
	if err != nil {
		return nil, err
	}

	return &info, nil
}