//+------------------------------------------------------------------+
//|                          LWMA 5/30 Dashboard (Final FIXED)       |
//+------------------------------------------------------------------+
#property strict

input ENUM_BASE_CORNER DashboardCorner = CORNER_RIGHT_UPPER;
input int DashboardX = 10;
input int DashboardY = 20;
input int LWMA_Period_1 = 5;
input int LWMA_Period_2 = 30;
input int Update_Interval_Seconds = 60;
input string Symbols = "EURUSD,USDJPY,XAUUSD";
input string TimeframeList = "M5,M15,M30,H1,H4,D1,W1,MN";

string Timeframes[];
color Color_Above = clrDodgerBlue;
color Color_Between = clrGray;
color Color_Below = clrCrimson;

datetime lastUpdate = 0;

int OnInit()
{
   EventSetTimer(Update_Interval_Seconds);
   DrawDashboard();
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
   EventKillTimer();
   DeleteDashboardObjects();
}

void OnTimer()
{
   if (TimeCurrent() - lastUpdate >= Update_Interval_Seconds)
   {
      DrawDashboard();
      lastUpdate = TimeCurrent();
   }
}

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
{
   if (id == CHARTEVENT_OBJECT_CLICK)
   {
      if (StringFind(sparam, "LWMA_DASH_SWITCH_") == 0)
      {
         string parts[];
         StringSplit(sparam, '_', parts);
         if (ArraySize(parts) == 5)
         {
            string sym = parts[3];
            string tf = parts[4];
            int tf_int = TFStringToInt(tf);
            if (tf_int > 0)
               ChartSetSymbolPeriod(0, sym, tf_int);
         }
      }
   }
}

// ============ Draw Dashboard ============
void DrawDashboard()
{
   DeleteDashboardObjects();

   StringSplit(TimeframeList, ',', Timeframes);
   string pairs[];
   int symbolCount = StringSplit(Symbols, ',', pairs);

   int w = 60, h = 20;
   int symbolWidth = 90;
   int totalWidth = symbolWidth + ArraySize(Timeframes) * w;
   int totalHeight = (symbolCount + 1) * h;

   int x = DashboardX;
   int y = DashboardY;

   if (DashboardCorner == CORNER_RIGHT_UPPER || DashboardCorner == CORNER_RIGHT_LOWER)
      x = ChartGetInteger(0, CHART_WIDTH_IN_PIXELS, 0) - DashboardX - totalWidth;
   if (DashboardCorner == CORNER_LEFT_LOWER || DashboardCorner == CORNER_RIGHT_LOWER)
      y = ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS, 0) - DashboardY - totalHeight;

   // Dashboard title
   string titleObj = "LWMA_DASH_TITLE";
   ObjectCreate(0, titleObj, OBJ_LABEL, 0, 0, 0);
   ObjectSetInteger(0, titleObj, OBJPROP_CORNER, DashboardCorner);
   ObjectSetInteger(0, titleObj, OBJPROP_XDISTANCE, x);
   ObjectSetInteger(0, titleObj, OBJPROP_YDISTANCE, y - 25);
   ObjectSetText(titleObj, "LWMA " + IntegerToString(LWMA_Period_1) + "/" + IntegerToString(LWMA_Period_2) + " DASH", 14, "Arial Bold", clrBlack);

   // Dashboard frame
   string frameBox = "LWMA_DASH_FRAME";
   ObjectCreate(0, frameBox, OBJ_RECTANGLE_LABEL, 0, 0, 0);
   ObjectSetInteger(0, frameBox, OBJPROP_CORNER, DashboardCorner);
   ObjectSetInteger(0, frameBox, OBJPROP_XDISTANCE, x - 2);
   ObjectSetInteger(0, frameBox, OBJPROP_YDISTANCE, y - 2);
   ObjectSetInteger(0, frameBox, OBJPROP_XSIZE, totalWidth + 4);
   ObjectSetInteger(0, frameBox, OBJPROP_YSIZE, totalHeight + 4);
   ObjectSetInteger(0, frameBox, OBJPROP_BGCOLOR, clrWhiteSmoke);
   ObjectSetInteger(0, frameBox, OBJPROP_BORDER_COLOR, clrBlack);
   ObjectSetInteger(0, frameBox, OBJPROP_WIDTH, 2);
   ObjectSetInteger(0, frameBox, OBJPROP_BACK, true);

   // Header Row
   for (int c = 0; c <= ArraySize(Timeframes); c++)
   {
      string label = (c == 0) ? "Symbol" : Timeframes[c - 1];
      int cellX = x + (c == 0 ? 0 : symbolWidth + (c - 1) * w);
      DrawCell("HDR_" + label, cellX, y, (c == 0) ? symbolWidth : w, h, label, clrYellow, clrBlack);
   }

   // Grid Rows
   for (int r = 0; r < symbolCount; r++)
   {
      string sym = pairs[r];
      int rowY = y + (r + 1) * h;

      DrawCell("SYM_" + sym, x, rowY, symbolWidth, h, sym, clrYellow, clrBlack);

      for (int c = 0; c < ArraySize(Timeframes); c++)
      {
         string tf = Timeframes[c];
         int tf_int = TFStringToInt(tf);
         double price = iClose(sym, tf_int, 0);
         double ma1 = CustomLWMA_OC(sym, tf_int, LWMA_Period_1);
         double ma2 = CustomLWMA_OC(sym, tf_int, LWMA_Period_2);

         color boxColor = (price > ma1 && price > ma2) ? Color_Above :
                          (price < ma1 && price < ma2) ? Color_Below : Color_Between;

         string switchID = "SWITCH_" + sym + "_" + tf;
         int cellX = x + symbolWidth + c * w;
         string tooltip = 
            "Price=" + DoubleToString(price, _Digits) +
            ", MA" + IntegerToString(LWMA_Period_1) + "=" + DoubleToString(ma1, _Digits) +
            ", MA" + IntegerToString(LWMA_Period_2) + "=" + DoubleToString(ma2, _Digits);

         DrawCell(switchID, cellX, rowY, w, h, "", clrWhite, boxColor);
         ObjectSetString(0, switchID, OBJPROP_TOOLTIP, tooltip);
      }
   }
}

// ============ Draw Cell ============
void DrawCell(string id, int x, int y, int w, int h, string text, color textColor, color bgColor)
{
   string boxName = "LWMA_DASH_" + id;
   string textName = boxName + "_LBL";

   ObjectCreate(0, boxName, OBJ_RECTANGLE_LABEL, 0, 0, 0);
   ObjectSetInteger(0, boxName, OBJPROP_CORNER, DashboardCorner);
   ObjectSetInteger(0, boxName, OBJPROP_XDISTANCE, x);
   ObjectSetInteger(0, boxName, OBJPROP_YDISTANCE, y);
   ObjectSetInteger(0, boxName, OBJPROP_XSIZE, w);
   ObjectSetInteger(0, boxName, OBJPROP_YSIZE, h);
   ObjectSetInteger(0, boxName, OBJPROP_BGCOLOR, bgColor);
   ObjectSetInteger(0, boxName, OBJPROP_BORDER_COLOR, clrBlack);
   ObjectSetInteger(0, boxName, OBJPROP_WIDTH, 1);
   ObjectSetInteger(0, boxName, OBJPROP_BACK, true);

   if (text != "")
   {
      ObjectCreate(0, textName, OBJ_LABEL, 0, 0, 0);
      ObjectSetInteger(0, textName, OBJPROP_CORNER, DashboardCorner);
      ObjectSetInteger(0, textName, OBJPROP_XDISTANCE, x + 2);
      ObjectSetInteger(0, textName, OBJPROP_YDISTANCE, y + 2);
      ObjectSetText(textName, text, 12, "Arial", textColor);
   }
}

// ============ Cleanup ============
void DeleteDashboardObjects()
{
   int total = ObjectsTotal();
   for (int i = total - 1; i >= 0; i--)
   {
      string name = ObjectName(i);
      if (StringFind(name, "LWMA_DASH_") == 0)
         ObjectDelete(name);
   }
}

// ============ Custom LWMA ============
double CustomLWMA_OC(string sym, int tf, int period)
{
   double sum = 0.0;
   double weightSum = 0.0;
   for (int i = 0; i < period; i++)
   {
      double mid = (iOpen(sym, tf, i) + iClose(sym, tf, i)) / 2.0;
      int weight = period - i;
      sum += mid * weight;
      weightSum += weight;
   }
   return (weightSum > 0) ? sum / weightSum : 0.0;
}

// ============ Timeframe Mapping ============
int TFStringToInt(string tf)
{
   if (tf == "M1") return PERIOD_M1;
   if (tf == "M5") return PERIOD_M5;
   if (tf == "M15") return PERIOD_M15;
   if (tf == "M30") return PERIOD_M30;
   if (tf == "H1") return PERIOD_H1;
   if (tf == "H4") return PERIOD_H4;
   if (tf == "D1") return PERIOD_D1;
   if (tf == "W1") return PERIOD_W1;
   if (tf == "MN") return PERIOD_MN1;
   return PERIOD_CURRENT;
}
