# indie:lang_version = 5
# Ripster EMA Clouds — Indie port
# Original Pine Script by ripster47 (© ripster47)
# License: Mozilla Public License 2.0
# Migration notes:
#   offset= parameter not supported in @plot.line; default=0 so no visual difference
#   alertcondition() — no Indie equivalent
#   Both EMA and SMA series called unconditionally; selection via matype param
#   Dynamic fill and line colors computed per-bar

from math import nan, isnan
from indie import indicator, param, plot, MainContext, color, source
from indie.algorithms import Ema, Sma

@indicator('Ripster EMA Clouds', overlay_main_pane=True)
@param.str('matype',   default='EMA',  title='MA Type: EMA/SMA')
@param.source('src',   default=source.HL2, title='Source')
@param.int('len1s',  default=8,   min=1, title='Cloud-1 Short Length')
@param.int('len1l',  default=9,   min=1, title='Cloud-1 Long Length')
@param.int('len2s',  default=5,   min=1, title='Cloud-2 Short Length')
@param.int('len2l',  default=12,  min=1, title='Cloud-2 Long Length')
@param.int('len3s',  default=34,  min=1, title='Cloud-3 Short Length')
@param.int('len3l',  default=50,  min=1, title='Cloud-3 Long Length')
@param.int('len4s',  default=72,  min=1, title='Cloud-4 Short Length')
@param.int('len4l',  default=89,  min=1, title='Cloud-4 Long Length')
@param.int('len5s',  default=180, min=1, title='Cloud-5 Short Length')
@param.int('len5l',  default=200, min=1, title='Cloud-5 Long Length')
@param.bool('show_line', default=False, title='Display MA Lines')
@param.bool('show1', default=True,  title='Show Cloud-1 (8/9)')
@param.bool('show2', default=True,  title='Show Cloud-2 (5/12)')
@param.bool('show3', default=True,  title='Show Cloud-3 (34/50)')
@param.bool('show4', default=False, title='Show Cloud-4 (72/89)')
@param.bool('show5', default=False, title='Show Cloud-5 (180/200)')
@plot.line('s1', color=color.rgba(3,  97,  3,   1.0), line_width=1, title='Short MA1')
@plot.line('l1', color=color.rgba(3,  97,  3,   1.0), line_width=2, title='Long MA1')
@plot.fill('s1', 'l1', id='cloud1')
@plot.line('s2', color=color.rgba(76, 175, 80,  1.0), line_width=1, title='Short MA2')
@plot.line('l2', color=color.rgba(76, 175, 80,  1.0), line_width=2, title='Long MA2')
@plot.fill('s2', 'l2', id='cloud2')
@plot.line('s3', color=color.rgba(33, 150, 243, 1.0), line_width=1, title='Short MA3')
@plot.line('l3', color=color.rgba(33, 150, 243, 1.0), line_width=2, title='Long MA3')
@plot.fill('s3', 'l3', id='cloud3')
@plot.line('s4', color=color.rgba(0,  150, 136, 1.0), line_width=1, title='Short MA4')
@plot.line('l4', color=color.rgba(0,  150, 136, 1.0), line_width=2, title='Long MA4')
@plot.fill('s4', 'l4', id='cloud4')
@plot.line('s5', color=color.rgba(5,  190, 213, 1.0), line_width=1, title='Short MA5')
@plot.line('l5', color=color.rgba(5,  190, 213, 1.0), line_width=2, title='Long MA5')
@plot.fill('s5', 'l5', id='cloud5')
class Main(MainContext):
    def calc(self, matype, src, len1s, len1l, len2s, len2l, len3s, len3l, len4s, len4l, len5s, len5l,
             show_line, show1, show2, show3, show4, show5):
        # All series computed unconditionally (Indie requirement)
        e1s = Ema.new(src, len1s);  e1l = Ema.new(src, len1l)
        e2s = Ema.new(src, len2s);  e2l = Ema.new(src, len2l)
        e3s = Ema.new(src, len3s);  e3l = Ema.new(src, len3l)
        e4s = Ema.new(src, len4s);  e4l = Ema.new(src, len4l)
        e5s = Ema.new(src, len5s);  e5l = Ema.new(src, len5l)
        m1s = Sma.new(src, len1s);  m1l = Sma.new(src, len1l)
        m2s = Sma.new(src, len2s);  m2l = Sma.new(src, len2l)
        m3s = Sma.new(src, len3s);  m3l = Sma.new(src, len3l)
        m4s = Sma.new(src, len4s);  m4l = Sma.new(src, len4l)
        m5s = Sma.new(src, len5s);  m5l = Sma.new(src, len5l)

        use_ema: bool = matype.upper() != 'SMA'

        sv1: float = e1s[0] if use_ema else m1s[0];  lv1: float = e1l[0] if use_ema else m1l[0]
        sv2: float = e2s[0] if use_ema else m2s[0];  lv2: float = e2l[0] if use_ema else m2l[0]
        sv3: float = e3s[0] if use_ema else m3s[0];  lv3: float = e3l[0] if use_ema else m3l[0]
        sv4: float = e4s[0] if use_ema else m4s[0];  lv4: float = e4l[0] if use_ema else m4l[0]
        sv5: float = e5s[0] if use_ema else m5s[0];  lv5: float = e5l[0] if use_ema else m5l[0]

        sp1: float = e1s[1] if use_ema else m1s[1];  lp1: float = e1l[1] if use_ema else m1l[1]
        sp2: float = e2s[1] if use_ema else m2s[1];  lp2: float = e2l[1] if use_ema else m2l[1]
        sp3: float = e3s[1] if use_ema else m3s[1];  lp3: float = e3l[1] if use_ema else m3l[1]
        sp4: float = e4s[1] if use_ema else m4s[1];  lp4: float = e4l[1] if use_ema else m4l[1]
        sp5: float = e5s[1] if use_ema else m5s[1];  lp5: float = e5l[1] if use_ema else m5l[1]

        # Fill colors (Pine transparency 45->alpha 0.55, 65->0.35, 70->0.30)
        fill1 = (color.rgba(3, 97, 3, 0.55)    if sv1 >= lv1 else color.rgba(136, 14, 79, 0.55))  if show1 else color.TRANSPARENT
        fill2 = (color.rgba(76, 175, 80, 0.35)  if sv2 >= lv2 else color.rgba(244, 67, 54, 0.35))  if show2 else color.TRANSPARENT
        fill3 = (color.rgba(33, 150, 243, 0.30) if sv3 >= lv3 else color.rgba(255, 183, 77, 0.30)) if show3 else color.TRANSPARENT
        fill4 = (color.rgba(0, 150, 136, 0.35)  if sv4 >= lv4 else color.rgba(240, 98, 146, 0.35)) if show4 else color.TRANSPARENT
        fill5 = (color.rgba(5, 190, 213, 0.35)  if sv5 >= lv5 else color.rgba(230, 81, 0, 0.35))   if show5 else color.TRANSPARENT

        # Line colors: olive=rising/maroon=falling for short; green=rising/red=falling for long
        olive  = color.rgba(128, 128, 0, 1.0)
        maroon = color.rgba(128, 0,   0, 1.0)
        trans  = color.TRANSPARENT

        sc1 = (olive if (not isnan(sp1) and sv1 >= sp1) else maroon) if (show_line and show1) else trans
        lc1 = (color.GREEN if (not isnan(lp1) and lv1 >= lp1) else color.RED) if (show_line and show1) else trans
        sc2 = (olive if (not isnan(sp2) and sv2 >= sp2) else maroon) if (show_line and show2) else trans
        lc2 = (color.GREEN if (not isnan(lp2) and lv2 >= lp2) else color.RED) if (show_line and show2) else trans
        sc3 = (olive if (not isnan(sp3) and sv3 >= sp3) else maroon) if (show_line and show3) else trans
        lc3 = (color.GREEN if (not isnan(lp3) and lv3 >= lp3) else color.RED) if (show_line and show3) else trans
        sc4 = (olive if (not isnan(sp4) and sv4 >= sp4) else maroon) if (show_line and show4) else trans
        lc4 = (color.GREEN if (not isnan(lp4) and lv4 >= lp4) else color.RED) if (show_line and show4) else trans
        sc5 = (olive if (not isnan(sp5) and sv5 >= sp5) else maroon) if (show_line and show5) else trans
        lc5 = (color.GREEN if (not isnan(lp5) and lv5 >= lp5) else color.RED) if (show_line and show5) else trans

        # Plot values: nan when cloud disabled
        s1v: float = sv1 if show1 else nan;  l1v: float = lv1 if show1 else nan
        s2v: float = sv2 if show2 else nan;  l2v: float = lv2 if show2 else nan
        s3v: float = sv3 if show3 else nan;  l3v: float = lv3 if show3 else nan
        s4v: float = sv4 if show4 else nan;  l4v: float = lv4 if show4 else nan
        s5v: float = sv5 if show5 else nan;  l5v: float = lv5 if show5 else nan

        return (
            plot.Line(s1v, color=sc1),
            plot.Line(l1v, color=lc1),
            plot.Fill(fill1),
            plot.Line(s2v, color=sc2),
            plot.Line(l2v, color=lc2),
            plot.Fill(fill2),
            plot.Line(s3v, color=sc3),
            plot.Line(l3v, color=lc3),
            plot.Fill(fill3),
            plot.Line(s4v, color=sc4),
            plot.Line(l4v, color=lc4),
            plot.Fill(fill4),
            plot.Line(s5v, color=sc5),
            plot.Line(l5v, color=lc5),
            plot.Fill(fill5),
        )
