{
  "$schema": "../../packages/ui/screens/screen.schema.json",
  "id": "athlete.plans-programme",
  "version": 1,
  "app": "athlete",
  "route": "/plaene/:id",
  "title": {
    "$bind": "plan.title"
  },
  "data": {
    "plan": {
      "source": "athlete.programmes.today",
      "params": {
        "id": {
          "$bind": "params.id"
        }
      }
    }
  },
  "chrome": {
    "title": {
      "$bind": "plan.title"
    },
    "back": {
      "label": "Pläne",
      "href": "?screen=athlete.plans"
    }
  },
  "actions": {
    "done": {
      "handler": "athlete.training.tickExercise",
      "risk": "reversible",
      "success": {
        "$if": {
          "$eq": [
            {
              "$count": "plan.exercises",
              "as": "x",
              "where": {
                "$bind": "x.done"
              }
            },
            0
          ]
        },
        "then": "Noch 4 Übungen",
        "else": {
          "$if": {
            "$eq": [
              {
                "$count": "plan.exercises",
                "as": "x",
                "where": {
                  "$bind": "x.done"
                }
              },
              1
            ]
          },
          "then": "Noch 3 Übungen",
          "else": {
            "$if": {
              "$eq": [
                {
                  "$count": "plan.exercises",
                  "as": "x",
                  "where": {
                    "$bind": "x.done"
                  }
                },
                2
              ]
            },
            "then": "Noch 2 Übungen",
            "else": {
              "$if": {
                "$eq": [
                  {
                    "$count": "plan.exercises",
                    "as": "x",
                    "where": {
                      "$bind": "x.done"
                    }
                  },
                  3
                ]
              },
              "then": "Noch eine Übung!"
            }
          }
        }
      },
      "celebrate": {
        "kind": {
          "$if": {
            "$eq": [
              {
                "$count": "plan.exercises",
                "as": "x",
                "where": {
                  "$bind": "x.done"
                }
              },
              4
            ]
          },
          "then": "huge",
          "else": "exercise"
        },
        "style": {
          "$if": {
            "$flag": "athlete.calm-celebrations"
          },
          "then": "calm",
          "else": "full"
        },
        "pref": {
          "$flag": "athlete.celebration-setting"
        },
        "points": {
          "$if": {
            "$eq": [
              {
                "$count": "plan.exercises",
                "as": "x",
                "where": {
                  "$bind": "x.done"
                }
              },
              4
            ]
          },
          "then": {
            "$bind": "plan.points"
          }
        },
        "message": {
          "$if": {
            "$eq": [
              {
                "$count": "plan.exercises",
                "as": "x",
                "where": {
                  "$bind": "x.done"
                }
              },
              4
            ]
          },
          "then": "🏋️ Training geschafft!",
          "else": [
            "Übung geschafft! 💪",
            "Stark! 🔥",
            "Boom! 👊",
            "Weiter so! ⭐",
            "Läuft! 🥋",
            "Yes! ✨",
            "Nächste! 🚀"
          ]
        }
      },
      "next": {
        "$join": [
          {
            "$text": "?screen=athlete.plans-programme&id={params.id}&e1={params.e1}&e2={params.e2}&e3={params.e3}&e4={params.e4}&e5={params.e5}&set={params.set}&{target.id}=done"
          },
          "&reply=",
          {
            "$fmt": "url",
            "value": {
              "$bind": "params.reply"
            }
          }
        ]
      }
    },
    "set": {
      "handler": "athlete.training.tickSet",
      "risk": "reversible",
      "next": {
        "$join": [
          {
            "$text": "?screen=athlete.plans-programme&id={params.id}&e1={params.e1}&e2={params.e2}&e3={params.e3}&e4={params.e4}&e5={params.e5}&set={params.set}&set={target.id},{params.set}"
          },
          "&reply=",
          {
            "$fmt": "url",
            "value": {
              "$bind": "params.reply"
            }
          }
        ]
      },
      "success": {
        "$find": "plan.exercises",
        "as": "x",
        "where": {
          "$eq": [
            {
              "$bind": "x.id"
            },
            {
              "$bind": "target.id"
            }
          ]
        },
        "get": "nextSet"
      }
    },
    "reset": {
      "handler": "athlete.training.resetExercise",
      "risk": "reversible",
      "next": {
        "$join": [
          {
            "$text": "?screen=athlete.plans-programme&id={params.id}&e1={params.e1}&e2={params.e2}&e3={params.e3}&e4={params.e4}&e5={params.e5}&set={params.set}&{target.id}=reset"
          },
          "&reply=",
          {
            "$fmt": "url",
            "value": {
              "$bind": "params.reply"
            }
          }
        ]
      }
    },
    "reply": {
      "handler": "athlete.coach.reply",
      "risk": "safe",
      "success": "Antwort gesendet",
      "next": {
        "$join": [
          {
            "$text": "?screen=athlete.plans-programme&id={params.id}&e1={params.e1}&e2={params.e2}&e3={params.e3}&e4={params.e4}&e5={params.e5}&set={params.set}"
          },
          "&reply=",
          {
            "$fmt": "url",
            "value": {
              "$bind": "values.reply"
            }
          }
        ]
      }
    },
    "pdf": {
      "handler": "athlete.plans.pdf",
      "risk": "safe",
      "success": "PDF gespeichert."
    }
  },
  "body": [
    {
      "component": "page-header",
      "data": {
        "title": {
          "$bind": "plan.title"
        }
      }
    },
    {
      "layout": "card",
      "children": [
        {
          "layout": "cluster",
          "grow": "last",
          "children": [
            {
              "component": "ring",
              "data": {
                "value": {
                  "$bind": "plan.pct"
                },
                "label": {
                  "$text": "Programm zu {plan.pct} Prozent erledigt"
                }
              }
            },
            {
              "layout": "cluster",
              "children": [
                {
                  "component": "badge",
                  "data": {
                    "label": {
                      "$text": "Woche {plan.week} von {plan.weeks}"
                    }
                  }
                },
                {
                  "component": "badge",
                  "data": {
                    "label": {
                      "$bind": "plan.phase"
                    }
                  }
                },
                {
                  "component": "badge",
                  "data": {
                    "label": {
                      "$if": {
                        "$eq": [
                          {
                            "$count": "plan.exercises",
                            "as": "x",
                            "where": {
                              "$bind": "x.done"
                            }
                          },
                          {
                            "$bind": "plan.total"
                          }
                        ]
                      },
                      "then": "2/3 diese Woche",
                      "else": {
                        "$text": "{plan.weekDone}/{plan.weekTarget} diese Woche"
                      }
                    }
                  }
                },
                {
                  "component": "badge",
                  "data": {
                    "label": {
                      "$text": "{plan.weekStreak} Wo."
                    },
                    "icon": "flame"
                  }
                },
                {
                  "component": "button",
                  "data": {
                    "label": "PDF",
                    "icon": "download",
                    "size": "sm",
                    "variant": "ghost",
                    "action": "pdf"
                  }
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "component": "progress",
      "data": {
        "id": "today",
        "label": "Heute",
        "hideLabel": true,
        "value": {
          "$count": "plan.exercises",
          "as": "x",
          "where": {
            "$bind": "x.done"
          }
        },
        "max": {
          "$bind": "plan.total"
        },
        "text": {
          "$if": {
            "$eq": [
              {
                "$count": "plan.exercises",
                "as": "x",
                "where": {
                  "$bind": "x.done"
                }
              },
              {
                "$bind": "plan.total"
              }
            ]
          },
          "then": "Heutiges Training geschafft",
          "else": {
            "$join": [
              {
                "$count": "plan.exercises",
                "as": "x",
                "where": {
                  "$bind": "x.done"
                }
              },
              "/",
              {
                "$bind": "plan.total"
              },
              " Übungen heute · ",
              {
                "$bind": "plan.phaseLeft"
              }
            ]
          }
        }
      }
    },
    {
      "component": "alert",
      "data": {
        "tone": "neutral",
        "title": {
          "$bind": "plan.hint"
        }
      }
    },
    {
      "layout": "section",
      "id": "uebungen",
      "title": "Deine Übungen",
      "gap": "sm",
      "children": [
        {
          "component": "list",
          "data": {
            "label": "Deine Übungen",
            "gestures": "primary",
            "swipeHint": {
              "id": "athlete-swipe",
              "title": "So geht's",
              "items": [
                "Nach rechts wischen: Übung fertig",
                "Nach links wischen: ein Satz geschafft",
                "Name antippen: Bilder, Video und Beschreibung",
                "Erledigt aus Versehen: ↺ setzt zurück"
              ]
            },
            "items": {
              "$each": "plan.exercises",
              "as": "ex",
              "sortBy": "ex.done",
              "map": {
                "id": {
                  "$bind": "ex.id"
                },
                "title": {
                  "$bind": "ex.title"
                },
                "meta": {
                  "$bind": "ex.scheme"
                },
                "href": {
                  "$text": "#sheet-{ex.id}"
                },
                "done": {
                  "$bind": "ex.done"
                },
                "progress": {
                  "$if": {
                    "$gte": [
                      {
                        "$bind": "ex.sets"
                      },
                      2
                    ]
                  },
                  "then": {
                    "done": {
                      "$bind": "ex.setsDone"
                    },
                    "total": {
                      "$bind": "ex.sets"
                    }
                  }
                },
                "quickAction": {
                  "action": "done",
                  "label": "Fertig",
                  "icon": "check"
                },
                "doneAction": {
                  "action": "reset",
                  "label": "Zurücksetzen",
                  "icon": "undo"
                },
                "swipe": {
                  "$if": {
                    "$bind": "ex.done"
                  },
                  "else": {
                    "mode": "commit",
                    "start": {
                      "action": "done",
                      "label": "Übung fertig",
                      "icon": "check",
                      "tone": "success"
                    },
                    "end": {
                      "$if": {
                        "$gte": [
                          {
                            "$bind": "ex.sets"
                          },
                          2
                        ]
                      },
                      "then": {
                        "action": "set",
                        "label": "Satz geschafft",
                        "icon": "plus",
                        "tone": "accent"
                      }
                    }
                  }
                },
                "actions": {
                  "$if": {
                    "$and": [
                      {
                        "$not": {
                          "$bind": "ex.done"
                        }
                      },
                      {
                        "$gte": [
                          {
                            "$bind": "ex.sets"
                          },
                          2
                        ]
                      }
                    ]
                  },
                  "then": [
                    {
                      "action": "set",
                      "label": "Ein Satz geschafft",
                      "icon": "plus"
                    }
                  ],
                  "else": []
                }
              }
            }
          }
        }
      ]
    },
    {
      "each": "plan.exercises",
      "as": "ex",
      "node": {
        "component": "sheet",
        "data": {
          "id": {
            "$text": "sheet-{ex.id}"
          },
          "title": {
            "$bind": "ex.title"
          },
          "meta": {
            "$bind": "ex.sheetMeta"
          },
          "paragraphs": [
            {
              "$bind": "ex.description"
            }
          ]
        }
      }
    },
    {
      "layout": "section",
      "variant": "label",
      "id": "coach",
      "title": "Vom Coach",
      "gap": "sm",
      "children": [
        {
          "each": "plan.coach",
          "as": "m",
          "node": {
            "component": "note",
            "data": {
              "author": {
                "$bind": "m.author"
              },
              "date": {
                "$bind": "m.date"
              },
              "dateLabel": {
                "$bind": "m.dateLabel"
              },
              "text": [
                {
                  "$bind": "m.text"
                }
              ],
              "mine": {
                "$bind": "m.mine"
              }
            }
          }
        },
        {
          "component": "note",
          "if": {
            "$bind": "params.reply"
          },
          "data": {
            "author": "Du",
            "dateLabel": "gerade eben",
            "text": [
              {
                "$bind": "params.reply"
              }
            ],
            "mine": true
          }
        },
        {
          "layout": "cluster",
          "grow": "first",
          "children": [
            {
              "component": "field",
              "data": {
                "id": "reply",
                "name": "reply",
                "label": "Antwort an Sandra",
                "hideLabel": true,
                "placeholder": "Antworten …",
                "maxlength": 600,
                "autocomplete": "off"
              }
            },
            {
              "component": "button",
              "data": {
                "label": "Senden",
                "icon": "send",
                "iconOnly": true,
                "ariaLabel": "Antwort senden",
                "variant": "primary",
                "action": "reply"
              }
            }
          ]
        }
      ]
    },
    {
      "layout": "section",
      "variant": "label",
      "id": "abzeichen",
      "title": "Meine Abzeichen",
      "gap": "sm",
      "children": [
        {
          "layout": "cluster",
          "children": [
            {
              "each": "plan.badges",
              "as": "b",
              "node": {
                "component": "badge",
                "data": {
                  "label": {
                    "$bind": "b.label"
                  },
                  "icon": {
                    "$bind": "b.icon"
                  }
                }
              }
            }
          ]
        },
        {
          "component": "kv",
          "data": {
            "items": [
              {
                "term": "Nächstes Abzeichen",
                "value": {
                  "$bind": "plan.nextBadge"
                }
              }
            ]
          }
        }
      ]
    },
    {
      "layout": "section",
      "variant": "label",
      "id": "gruppe",
      "title": "Ranking der Gruppe",
      "gap": "sm",
      "children": [
        {
          "component": "list",
          "data": {
            "label": "Ranking der Gruppe",
            "variant": "cards",
            "items": {
              "$each": "plan.group",
              "as": "g",
              "map": {
                "id": {
                  "$bind": "g.id"
                },
                "title": {
                  "$bind": "g.name"
                },
                "rank": {
                  "$bind": "g.rank"
                },
                "value": {
                  "$bind": "g.trainings"
                },
                "current": {
                  "$bind": "g.me"
                },
                "meta": {
                  "$if": {
                    "$bind": "g.streak"
                  },
                  "then": {
                    "$text": "{g.streak} Wochen am Stück"
                  }
                }
              }
            }
          }
        }
      ]
    }
  ]
}
