using TShockAPI;
using Newtonsoft.Json;

namespace AuctionHouse
{
    internal class DS // Data Structures
    {
        
        public class Listing
        {
            public int ID = -1;
            public int ItemID = -1;
            public int Amount = -1;
            public byte PrefixID = 0;

            public long Price; // Sell price
            public long Deposit; // Deposit that went into the listing. Should probably make this = Price * 0.05 or something.

            public DateTime ReturnDate = DateTime.Now.AddDays(7); // Items will be returned to the lister after 7 days if they are not sold, provided they join the server between this point and the expiration point.
            public DateTime ExpirationDate = DateTime.Now.AddDays(15); // Items will be removed from the auction house after 15 days.

            public int ListerAccountID = -1;
            public string ListerAccount = ""; // Name is needed for SEConomy bank, it doesn't work with IDs.

            #region Methods
            public string ItemName()
            {
                return TShock.Utils.GetItemById(ItemID).Name;
            }

            public string PrefixName() 
            { 
                return TShock.Utils.GetPrefixById(PrefixID); 
            }
            #endregion
        }

        public class Payment 
        {
            public int AccountID;
            public long AmountDue;
            public int TransactionID = -1;
            public string Reason = "";

            public DateTime DueBy = DateTime.Now.AddDays(15);

            public Payment(int accountID, long amountDue, int tID, string reason = "")
            {
                AccountID = accountID;
                AmountDue = amountDue;
                TransactionID = tID;


                Reason = reason;

            }
        }

        // Need these all to be saved to disk.
        public static int TotalListings = 0;
        public static readonly List<Listing> Listings = new List<Listing>(); // Holds all of the auction house's listings.
        public static readonly List<Payment> PaymentQueue = new List<Payment>(); // Holds all of the payments that need to be made.


        private class DC // Data Container
        {
            public int TotalListings { get; set; }
            public List<Listing> Listings { get; set; }
            public List<Payment> PaymentQueue { get; set; }
        }

        public static void SaveToDisk()
        {
            string filePath = Path.Combine(TShock.SavePath, "AuctionHouse.json");
            var data = new DC
            {
                TotalListings = DS.TotalListings,
                Listings = DS.Listings,
                PaymentQueue = DS.PaymentQueue
            };

            string json = JsonConvert.SerializeObject(data, Formatting.Indented);
            File.WriteAllText(filePath, json);
        }

        public static void LoadFromDisk()
        {
            string filePath = Path.Combine(TShock.SavePath, "AuctionHouse.json");

            if (!File.Exists(filePath))
            {
                // Create a new file with default data
                var defaultData = new DC
                {
                    TotalListings = 0,
                    Listings = new List<Listing>(),
                    PaymentQueue = new List<Payment>()
                };
                string defaultJson = JsonConvert.SerializeObject(defaultData, Formatting.Indented);
                File.WriteAllText(filePath, defaultJson);
            }

            string json = File.ReadAllText(filePath);
            var data = JsonConvert.DeserializeObject<DC>(json);

            TotalListings = data.TotalListings;
            Listings.Clear();
            Listings.AddRange(data.Listings);
            PaymentQueue.Clear();
            PaymentQueue.AddRange(data.PaymentQueue);
        }

       
    }
}