<?xml version="1.0" encoding="utf-8"?>
<!--
  TimeLimit Copyright <C> 2019 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:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".ui.lock.LockFragment">

    <data>
        <variable
            name="handlers"
            type="io.timelimit.android.ui.lock.Handlers" />

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

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

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

        <variable
            name="reason"
            type="io.timelimit.android.logic.BlockingReason" />

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

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

        <import type="android.view.View" />
        <import type="io.timelimit.android.logic.BlockingReason" />
        <import type="android.text.TextUtils" />
    </data>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ScrollView
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:padding="8dp"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:gravity="center_horizontal"
                    android:textAppearance="?android:textAppearanceLarge"
                    android:text="@string/lock_header_blocked"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

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

                <TextView
                    android:gravity="center_horizontal"
                    android:padding="4dp"
                    android:textAppearance="?android:textAppearanceLarge"
                    android:text="@string/lock_header_what"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <androidx.cardview.widget.CardView
                    app:cardUseCompatPadding="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:id="@+id/app_icon"
                            tools:src="@mipmap/ic_launcher"
                            android:layout_margin="8dp"
                            android:layout_gravity="center_vertical"
                            android:layout_width="48dp"
                            android:layout_height="48dp" />

                        <LinearLayout
                            android:layout_gravity="center_vertical"
                            android:padding="8dp"
                            android:orientation="vertical"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">
                            <TextView
                                android:textAppearance="?android:textAppearanceMedium"
                                tools:text="Evil App"
                                android:text="@{appTitle}"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content" />

                            <TextView
                                tools:text="verbotene Apps"
                                android:visibility="@{TextUtils.isEmpty(appCategoryTitle) ? View.GONE : View.VISIBLE}"
                                android:textAppearance="?android:textAppearanceMedium"
                                android:text="@{appCategoryTitle}"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content" />

                            <TextView
                                tools:text="com.evil.app"
                                android:textAppearance="?android:textAppearanceSmall"
                                android:text="@{packageName}"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content" />

                        </LinearLayout>
                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <TextView
                    android:gravity="center_horizontal"
                    android:padding="4dp"
                    android:textAppearance="?android:textAppearanceLarge"
                    android:text="@string/lock_header_why"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <androidx.cardview.widget.CardView
                    app:cardUseCompatPadding="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:padding="8dp"
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:visibility="@{reason == BlockingReason.NotPartOfAnCategory ? View.VISIBLE : View.GONE}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/lock_reason_no_category"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{reason == BlockingReason.TemporarilyBlocked ? View.VISIBLE : View.GONE}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/lock_reason_temporarily_blocked"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{reason == BlockingReason.BlockedAtThisTime ? View.VISIBLE : View.GONE}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/lock_reason_blocked_time"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{reason == BlockingReason.TimeOver ? View.VISIBLE : View.GONE}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/lock_reason_time_over"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{reason == BlockingReason.TimeOverExtraTimeCanBeUsedLater ? View.VISIBLE : View.GONE}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/lock_reason_time_over_etra_time_can_be_used_later"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{reason == BlockingReason.MissingNetworkTime ? View.VISIBLE : View.GONE}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/lock_reason_missing_network_time"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{reason == BlockingReason.RequiresCurrentDevice ? View.VISIBLE : View.GONE}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/lock_reason_requires_current_device"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <ProgressBar
                            android:visibility="@{reason == null ? View.VISIBLE : View.GONE}"
                            android:padding="8dp"
                            android:layout_gravity="center_horizontal"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <TextView
                    android:gravity="center_horizontal"
                    android:padding="4dp"
                    android:textAppearance="?android:textAppearanceLarge"
                    android:text="@string/lock_header_how_proceed"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <androidx.cardview.widget.CardView
                    android:visibility="@{reason == BlockingReason.NotPartOfAnCategory ? View.VISIBLE : View.GONE}"
                    android:foreground="?selectableItemBackground"
                    android:onClick="@{() -> handlers.allowTemporarily()}"
                    app:cardUseCompatPadding="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:orientation="vertical"
                        android:padding="8dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <TextView
                            android:text="@string/lock_allow_temporarily_header"
                            android:textAppearance="?android:textAppearanceLarge"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            tools:text="@string/lock_allow_temporarily_text"
                            android:text="@{@string/lock_allow_temporarily_text(appTitle)}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:visibility="@{reason == BlockingReason.NotPartOfAnCategory ? View.VISIBLE : View.GONE}"
                    app:cardUseCompatPadding="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:orientation="vertical"
                        android:padding="8dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <TextView
                            android:text="@string/lock_add_to_category_title"
                            android:textAppearance="?android:textAppearanceLarge"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:text="@string/lock_add_to_category_text"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <com.google.android.flexbox.FlexboxLayout
                            android:id="@+id/add_to_category_options"
                            app:flexWrap="wrap"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                            <!-- This is filled at runtime -->

                        </com.google.android.flexbox.FlexboxLayout>

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:visibility="@{reason == BlockingReason.MissingNetworkTime ? View.VISIBLE : View.GONE}"
                    app:cardUseCompatPadding="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:orientation="vertical"
                        android:padding="8dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <TextView
                            android:text="@string/lock_confirm_time_title"
                            android:textAppearance="?android:textAppearanceLarge"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:text="@{currentTime}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <Button
                            android:onClick="@{() -> handlers.confirmLocalTime()}"
                            android:text="@string/lock_confirm_time_btn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />
                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:foreground="?selectableItemBackground"
                    android:onClick="@{() -> handlers.disableTimeVerification()}"
                    android:visibility="@{reason == BlockingReason.MissingNetworkTime ? View.VISIBLE : View.GONE}"
                    app:cardUseCompatPadding="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:orientation="vertical"
                        android:padding="8dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <TextView
                            android:text="@string/lock_disable_time_verification_title"
                            android:textAppearance="?android:textAppearanceLarge"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:text="@string/lock_disable_time_verification_text"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:visibility="@{reason == BlockingReason.TimeOver ? View.VISIBLE : View.GONE}"
                    app:cardUseCompatPadding="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:orientation="vertical"
                        android:padding="8dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:text="@string/lock_extratime_title"
                            android:textAppearance="?android:textAppearanceLarge"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:text="@string/lock_extratime_text"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

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

                        <Button
                            android:id="@+id/extra_time_btn_ok"
                            android:text="@string/generic_ok"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:textAppearance="?android:textAppearanceSmall"
                            android:text="@string/purchase_required_info_local_mode_free"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:visibility="@{reason == BlockingReason.TemporarilyBlocked ? View.VISIBLE : View.GONE}"
                    app:cardUseCompatPadding="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:orientation="vertical"
                        android:padding="8dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <TextView
                            android:textAppearance="?android:textAppearanceLarge"
                            android:text="@string/lock_disable_temporarily_lock_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

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

                            <Button
                                android:onClick="@{() -> handlers.disableTemporarilyLockForCurrentCategory()}"
                                tools:text="Erlaubte Spiele"
                                android:text="@{appCategoryTitle}"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" />

                            <Button
                                android:enabled="@{safeUnbox(areMultipleCategoriesBlocked)}"
                                android:onClick="@{() -> handlers.disableTemporarilyLockForAllCategories()}"
                                android:text="@string/lock_disable_temporarily_all_categories"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" />

                        </LinearLayout>

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

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <io.timelimit.android.ui.view.ManageDisableTimelimitsView
                    android:visibility="@{reason == BlockingReason.BlockedAtThisTime || reason == BlockingReason.TimeOver || reason == BlockingReason.TimeOverExtraTimeCanBeUsedLater ? View.VISIBLE : View.GONE}"
                    android:id="@+id/manage_disable_time_limits"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <androidx.cardview.widget.CardView
                    android:visibility="@{reason == BlockingReason.RequiresCurrentDevice ? View.VISIBLE : View.GONE}"
                    android:foreground="?selectableItemBackground"
                    android:onClick="@{() -> handlers.setThisDeviceAsCurrentDevice()}"
                    app:cardUseCompatPadding="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:orientation="vertical"
                        android:padding="8dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:text="@string/lock_update_current_device_title"
                            android:textAppearance="?android:textAppearanceLarge"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:text="@string/lock_update_current_device_text"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:textAppearance="?android:textAppearanceSmall"
                            android:text="@string/purchase_required_info_only_networking"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:foreground="?selectableItemBackground"
                    android:onClick="@{() -> handlers.openMainApp()}"
                    app:cardUseCompatPadding="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:orientation="vertical"
                        android:padding="8dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:text="@string/lock_goto_main_title"
                            android:textAppearance="?android:textAppearanceLarge"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:text="@string/lock_goto_main_text"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

            </LinearLayout>
        </ScrollView>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:onClick="@{() -> handlers.showAuthenticationScreen()}"
            android:id="@+id/fab"
            app:fabSize="normal"
            android:src="@drawable/ic_lock_open_white_24dp"
            android:layout_margin="16dp"
            android:layout_gravity="end|bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>
