<!--
  TimeLimit Copyright <C> 2019 - 2024 Jonas Lochmann
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation version 3 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="io.timelimit.android.ui.manage.category.timelimit_rules.edit.EditTimeLimitRuleDialogFragment">

    <data>
        <variable
            name="isNewRule"
            type="Boolean" />

        <variable
            name="applyToExtraTime"
            type="Boolean" />

        <variable
            name="applyToWholeDay"
            type="Boolean" />

        <variable
            name="applyToZeroDays"
            type="boolean" />

        <variable
            name="isSelfLimitMode"
            type="boolean" />

        <variable
            name="blockCurrentSelfLimitationParams"
            type="boolean" />

        <variable
            name="handlers"
            type="io.timelimit.android.ui.manage.category.timelimit_rules.edit.Handlers" />

        <variable
            name="affectsMultipleDays"
            type="boolean" />

        <variable
            name="showWeeklyDailyOption"
            type="boolean" />

        <variable
            name="startTime"
            type="String" />

        <variable
            name="endTime"
            type="String" />

        <variable
            name="enableSessionDurationLimit"
            type="boolean" />

        <variable
            name="sessionLengthText"
            type="String" />

        <variable
            name="sessionBreakText"
            type="String" />

        <variable
            name="expireEnabled"
            type="boolean" />

        <variable
            name="expireDateText"
            type="String" />

        <variable
            name="supportsExpiringRules"
            type="boolean" />

        <import type="android.view.View" />
    </data>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scroll">
        <LinearLayout
            android:padding="8dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:textAppearance="?android:textAppearanceLarge"
                tools:text="@string/category_time_limit_rule_dialog_new"
                android:text="@{safeUnbox(isNewRule) ? @string/category_time_limit_rule_dialog_new : @string/category_time_limit_rule_dialog_edit}"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <include layout="@layout/view_select_days" android:id="@+id/day_selection" />

            <com.google.android.material.button.MaterialButtonToggleGroup
                android:visibility="@{showWeeklyDailyOption ? View.VISIBLE : View.GONE}"
                android:id="@+id/per_day_group"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <com.google.android.material.button.MaterialButton
                    android:id="@+id/type_per_day"
                    style="?materialButtonOutlinedStyle"
                    android:text="@string/category_time_limit_rules_per_day"
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
                <com.google.android.material.button.MaterialButton
                    android:id="@+id/type_per_week"
                    style="?materialButtonOutlinedStyle"
                    android:text="@string/category_time_limit_rules_per_week"
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </com.google.android.material.button.MaterialButtonToggleGroup>

            <io.timelimit.android.ui.view.SelectTimeSpanView
                android:id="@+id/time_span"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <com.google.android.material.switchmaterial.SwitchMaterial
                android:onClick="@{() -> handlers.updateApplyToExtraTime(!safeUnbox(applyToExtraTime))}"
                android:checked="@{safeUnbox(applyToExtraTime)}"
                android:text="@string/category_time_limit_rules_apply_to_extra_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <com.google.android.material.switchmaterial.SwitchMaterial
                android:onClick="@{() -> handlers.updateApplyToWholeDay(!safeUnbox(applyToWholeDay))}"
                android:checked="@{safeUnbox(applyToWholeDay)}"
                android:text="@string/category_time_limit_rules_apply_to_whole_day"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <com.google.android.flexbox.FlexboxLayout
                app:flexWrap="wrap"
                app:alignItems="center"
                android:visibility="@{safeUnbox(applyToWholeDay) ? View.GONE : View.VISIBLE}"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:textAppearance="?android:textAppearanceSmall"
                    android:paddingStart="0dp"
                    android:paddingEnd="8dp"
                    android:text="@string/category_time_limit_rules_apply_to_part_day_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <Button
                    style="?materialButtonOutlinedStyle"
                    android:onClick="@{() -> handlers.updateStartTime()}"
                    tools:text="10:00"
                    android:text="@{startTime}"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:textAppearance="?android:textAppearanceSmall"
                    android:padding="8dp"
                    android:text="@string/category_time_limit_rules_apply_to_part_day_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <Button
                    style="?materialButtonOutlinedStyle"
                    android:onClick="@{() -> handlers.updateEndTime()}"
                    tools:text="16:00"
                    android:text="@{endTime}"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:textAppearance="?android:textAppearanceMedium"
                    android:padding="8dp"
                    android:text="@string/category_time_limit_rules_apply_to_part_day_3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </com.google.android.flexbox.FlexboxLayout>

            <com.google.android.material.switchmaterial.SwitchMaterial
                android:onClick="@{() -> handlers.updateSessionDurationLimit(!safeUnbox(enableSessionDurationLimit))}"
                android:checked="@{enableSessionDurationLimit}"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/category_time_limit_rules_enable_session_limit" />

            <androidx.gridlayout.widget.GridLayout
                android:visibility="@{enableSessionDurationLimit ? View.VISIBLE : View.GONE}"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    app:layout_columnWeight="1"
                    app:layout_gravity="fill_vertical|fill_horizontal"
                    android:gravity="center_vertical"
                    android:paddingEnd="8dp"
                    android:paddingStart="0dp"
                    app:layout_row="1"
                    app:layout_column="1"
                    android:textAppearance="?android:textAppearanceSmall"
                    android:text="@string/category_time_limit_rules_session_limit_duration"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <Button
                    android:layout_marginEnd="4dp"
                    app:layout_columnWeight="0"
                    android:onClick="@{() -> handlers.updateSessionLength()}"
                    app:layout_gravity="center_vertical|fill_horizontal"
                    style="?materialButtonOutlinedStyle"
                    app:layout_row="1"
                    app:layout_column="3"
                    tools:text="199999 Minuten"
                    android:text="@{sessionLengthText}"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    app:layout_columnWeight="1"
                    app:layout_gravity="fill_vertical|fill_horizontal"
                    android:gravity="center_vertical"
                    android:paddingEnd="8dp"
                    android:paddingStart="0dp"
                    android:textAppearance="?android:textAppearanceSmall"
                    app:layout_row="2"
                    app:layout_column="1"
                    android:text="@string/category_time_limit_rules_session_limit_pause"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content" />

                <Button
                    android:layout_marginEnd="4dp"
                    app:layout_columnWeight="0"
                    android:onClick="@{() -> handlers.updateSessionBreak()}"
                    app:layout_gravity="center_vertical|fill_horizontal"
                    style="?materialButtonOutlinedStyle"
                    app:layout_row="2"
                    app:layout_column="3"
                    tools:text="20 Minuten"
                    android:text="@{sessionBreakText}"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </androidx.gridlayout.widget.GridLayout>

            <com.google.android.material.switchmaterial.SwitchMaterial
                android:enabled="@{supportsExpiringRules}"
                android:onClick="@{() -> handlers.updateExpireEnabled(!safeUnbox(expireEnabled))}"
                android:checked="@{expireEnabled}"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/category_time_limit_rules_enable_expires" />

            <LinearLayout
                android:visibility="@{expireEnabled ? View.VISIBLE : View.GONE}"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:text="@string/category_time_limit_rules_expires_at"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <Button
                    android:layout_marginStart="16dp"
                    android:onClick="@{() -> handlers.updateExpireDate()}"
                    style="?materialButtonOutlinedStyle"
                    tools:text="01.01.2025 13:00 am"
                    android:text="@{expireDateText}"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <TextView
                android:visibility="@{safeUnbox(applyToWholeDay) ? View.GONE : View.VISIBLE}"
                android:textAppearance="?android:textAppearanceSmall"
                android:text="@string/category_time_limit_rules_warning_day_part"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:paddingTop="4dp"
                android:paddingBottom="4dp"
                android:visibility="@{applyToZeroDays ? View.VISIBLE : View.GONE}"
                android:textAppearance="?android:textAppearanceSmall"
                android:textColor="@color/orange_text"
                android:text="@string/category_time_limit_rules_warning_zero_day_part"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:paddingTop="4dp"
                android:paddingBottom="4dp"
                android:visibility="@{blockCurrentSelfLimitationParams ? View.VISIBLE : View.GONE}"
                android:textAppearance="?android:textAppearanceSmall"
                android:textColor="@color/text_red"
                android:text="@string/category_time_limit_rules_error_self_limit_only_more_strict"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <View
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="0dp" />

                <Button
                    android:layout_marginEnd="4dp"
                    style="?borderlessButtonStyle"
                    android:onClick="@{() -> handlers.onDeleteRule()}"
                    android:visibility="@{safeUnbox(isNewRule) || isSelfLimitMode ? View.GONE : View.VISIBLE}"
                    android:textColor="@color/text_red"
                    android:text="@string/generic_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <Button
                    android:layout_marginEnd="4dp"
                    android:layout_marginStart="4dp"
                    android:onClick="@{() -> handlers.onSaveRule()}"
                    android:enabled="@{!blockCurrentSelfLimitationParams}"
                    tools:text="@string/generic_create"
                    android:text="@{safeUnbox(isNewRule) ? @string/generic_create : @string/generic_save}"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</layout>