//+------------------------------------------------------------------+
//|                                                  MiniChartsEA.mq5 |
//|                        Uses CChartMiniTiles (ChartMiniTilesClass) |
//+------------------------------------------------------------------+
#property copyright "2025"
#property link      "https://www.mql5.com/en/users/billionaire2024/seller"
#property version   "1.00"
#property description "Mini-charts EA using CChartMiniTiles class"

// --- Include the class header

#include <ChartMiniTiles.mqh>

//--- Inputs
input string MajorSymbols      = "EURUSD,GBPUSD,USDJPY,USDCHF,USDCAD,AUDUSD,NZDUSD"; // comma-separated base symbols
input int    BarsWidth         = 20;         // bars used to estimate tile pixel width
input int    TileHeightPx      = 112;        // tile height in pixels (class default is 112)
input int    HorizontalSpacing = 6;          // spacing between tiles (pixels)
input int    UpdateInterval    = 1000;       // update interval (ms)
input int    XOffset           = 10;         // left margin in pixels
input int    BottomOffset      = 40;         // distance from bottom of chart in pixels (bottom offset)
input int    ToggleButtonX     = 8;          // toggle button X position (pixels from left)
input int    ToggleButtonY     = 6;          // toggle button Y position (pixels from top)
input bool   DateScale         = false;      // show date scale on tiles
input bool   PriceScale        = false;      // show price scale on tiles
input int    ChartScale        = 2;          // chart scale (zoom) for tiles

//--- object instance
CChartMiniTiles tiles;

//--- internal
int pixelWidth = 120;

//+------------------------------------------------------------------+
//| Helper: estimate pixel width from BarsWidth                      |
//+------------------------------------------------------------------+
int CalculateChartWidthFromBars(int barsWidth)
{
   int mainChartWidth = (int)ChartGetInteger(0, CHART_WIDTH_IN_PIXELS);
   int visibleBars    = (int)ChartGetInteger(0, CHART_VISIBLE_BARS);

   if(visibleBars <= 0 || mainChartWidth <= 0)
      return MathMax(80, BarsWidth * 6); // fallback estimate

   return MathMax(80, barsWidth * mainChartWidth / visibleBars);
}

//+------------------------------------------------------------------+
//| Expert initialization                                            |
//+------------------------------------------------------------------+
int OnInit()
{
   // compute pixel width from BarsWidth heuristic
   pixelWidth = CalculateChartWidthFromBars(BarsWidth);

   // set toggle button position in class
   tiles.SetToggleButtonPos(ToggleButtonX, ToggleButtonY);

   // Initialize tiles (majorSymbols, width, height, xOffset, bottomOffset, spacing, period, dateScale, priceScale, chartScale)
   bool ok = tiles.Init(MajorSymbols,
                        pixelWidth,
                        TileHeightPx,
                        XOffset,
                        BottomOffset,
                        HorizontalSpacing,
                        PERIOD_M1,
                        DateScale,
                        PriceScale,
                        ChartScale);

   if(!ok)
   {
      // initialization failed (likely no symbols resolved). Print message and fail.
      Print("MiniChartsEA: tiles.Init() failed. Check Experts log for details (missing/resolution of symbols).");
      return(INIT_FAILED);
   }

   // Start a timer to call UpdateLayout periodically so tiles remain adaptive
   EventSetMillisecondTimer(UpdateInterval);

   Print("MiniChartsEA initialized.");
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Timer: update layout                                              |
//+------------------------------------------------------------------+
void OnTimer()
{
   tiles.UpdateLayout();
}

//+------------------------------------------------------------------+
//| Chart events - forward to tiles and handle chart-changes         |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   // Let the tiles class handle clicks (toggle button). If handled, stop processing.
   if(tiles.HandleEvent(id, sparam))
      return;

   // If main chart changed (resize / layout), reflow tiles
   if(id == CHARTEVENT_CHART_CHANGE)
      tiles.UpdateLayout();
}

//+------------------------------------------------------------------+
//| Deinitialization                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   EventKillTimer();
   tiles.Delete();
   Print("MiniChartsEA deinitialized.");
}

//+------------------------------------------------------------------+
