<!--
  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:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="io.timelimit.android.ui.manage.device.manage.ManageDeviceFragment">

    <data>
        <variable
            name="deviceTitle"
            type="String" />

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

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

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

        <variable
            name="networkTimeVerification"
            type="io.timelimit.android.data.model.NetworkTime" />

        <variable
            name="usageStatsAccess"
            type="io.timelimit.android.integration.platform.RuntimePermissionStatus" />

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

        <variable
            name="notificationAccessPermission"
            type="io.timelimit.android.integration.platform.NewPermissionStatus" />

        <variable
            name="protectionLevel"
            type="io.timelimit.android.integration.platform.ProtectionLevel" />

        <variable
            name="handlers"
            type="io.timelimit.android.ui.manage.device.manage.ManageDeviceFragmentHandlers" />

        <import type="android.view.View" />
        <import type="io.timelimit.android.data.model.NetworkTime" />
        <import type="io.timelimit.android.integration.platform.RuntimePermissionStatus" />
        <import type="io.timelimit.android.integration.platform.NewPermissionStatus" />
        <import type="io.timelimit.android.integration.platform.ProtectionLevel" />
        <import type="io.timelimit.android.BuildConfig" />
    </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">

                <include android:id="@+id/introduction"
                    layout="@layout/fragment_manage_device_introduction" />

                <include android:id="@+id/uninstall"
                    layout="@layout/manage_device_uninstall_card" />

                <include android:id="@+id/usage_stats_access_missing"
                    layout="@layout/usage_stats_permission_required_and_missing" />

                <include
                    android:id="@+id/manage_manipulation"
                    layout="@layout/manage_device_manipulation_view" />

                <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:onClick="@{() -> handlers.editDeviceTitle()}"
                            android:focusable="true"
                            android:clickable="true"
                            android:background="?selectableItemBackground"
                            android:drawableEnd="@drawable/ic_edit_black_24dp"
                            android:textAppearance="?android:textAppearanceLarge"
                            tools:text="Poor childs phone"
                            android:text="@{deviceTitle}"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:textAppearance="?android:textAppearanceMedium"
                            tools:text="Samsung Galaxy S3"
                            android:text="@{modelString}"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:text="@{addedAtString}"
                            android:textAppearance="?android:textAppearanceMedium"
                            tools:text="Added two days ago"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

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

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

                <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:textAppearance="?android:textAppearanceLarge"
                            android:text="@string/manage_device_current_user_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <Spinner
                            android:id="@+id/user_spinner"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

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

                <include android:id="@+id/default_user"
                    layout="@layout/manage_device_default_user" />

                <androidx.cardview.widget.CardView
                    android:visibility="@{safeUnbox(didAppDowngrade) ? View.VISIBLE : View.GONE}"
                    app:cardUseCompatPadding="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:background="@color/red"
                        android:orientation="vertical"
                        android:padding="8dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:textColor="@color/white"
                            android:textAppearance="?android:textAppearanceLarge"
                            android:text="@string/manage_device_downgrade_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:textColor="@color/white"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/manage_device_downgrade_text"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

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

                <androidx.cardview.widget.CardView
                    android:foreground="?selectableItemBackground"
                    android:clickable="@{safeUnbox(isThisDevice)}"
                    android:onClick="@{() -> handlers.manageDeviceAdmin()}"
                    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/manage_device_permission_device_admin_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{protectionLevel == ProtectionLevel.None ? View.VISIBLE : View.GONE}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/manage_device_permission_device_admin_text_disabled"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{protectionLevel == ProtectionLevel.SimpleDeviceAdmin ? View.VISIBLE : View.GONE}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/manage_device_permission_device_admin_text_simple"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{protectionLevel == ProtectionLevel.PasswordDeviceAdmin ? View.VISIBLE : View.GONE}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/manage_device_permission_device_admin_text_password"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{protectionLevel == ProtectionLevel.DeviceOwner ? View.VISIBLE : View.GONE}"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/manage_device_permission_device_admin_text_owner"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

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

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

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

                <androidx.cardview.widget.CardView
                    android:onClick="@{() -> handlers.openUsageStatsSettings()}"
                    android:foreground="?selectableItemBackground"
                    android:clickable="@{safeUnbox(isThisDevice) &amp;&amp; (usageStatsAccess != RuntimePermissionStatus.NotRequired)}"
                    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/manage_device_permissions_usagestats_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

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

                        <TextView
                            android:visibility="@{usageStatsAccess == RuntimePermissionStatus.Granted ? View.VISIBLE : View.GONE}"
                            android:textColor="@color/green"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/manage_device_permission_status_granted"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{usageStatsAccess == RuntimePermissionStatus.NotGranted ? View.VISIBLE : View.GONE}"
                            android:textColor="@color/red"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/manage_device_permission_status_not_granted"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{usageStatsAccess == RuntimePermissionStatus.NotRequired ? View.VISIBLE : View.GONE}"
                            android:textColor="@color/green"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/manage_device_permission_status_not_required"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{(( usageStatsAccess != RuntimePermissionStatus.NotRequired) &amp;&amp; (safeUnbox(isThisDevice))) ? View.VISIBLE : View.GONE}"
                            android:text="@string/manage_device_permission_tap_top_open_settings"
                            android:textAppearance="?android:textAppearanceSmall"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{((safeUnbox(isThisDevice) || (usageStatsAccess == RuntimePermissionStatus.NotRequired))) ? View.GONE : View.VISIBLE}"
                            android:text="@string/manage_device_permission_open_at_target_device"
                            android:textAppearance="?android:textAppearanceSmall"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

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

                <androidx.cardview.widget.CardView
                    android:onClick="@{() -> handlers.openNotificationAccessSettings()}"
                    android:foreground="?selectableItemBackground"
                    android:clickable="@{safeUnbox(isThisDevice) &amp;&amp; (notificationAccessPermission != NewPermissionStatus.NotSupported)}"
                    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/manage_device_permission_notification_access_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

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

                        <TextView
                            android:visibility="@{notificationAccessPermission == NewPermissionStatus.Granted ? View.VISIBLE : View.GONE}"
                            android:textColor="@color/green"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/manage_device_permission_status_granted"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{notificationAccessPermission == NewPermissionStatus.NotGranted ? View.VISIBLE : View.GONE}"
                            android:textColor="@color/red"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/manage_device_permission_status_not_granted"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{notificationAccessPermission == NewPermissionStatus.NotSupported ? View.VISIBLE : View.GONE}"
                            android:textColor="@color/red"
                            android:textAppearance="?android:textAppearanceMedium"
                            android:text="@string/manage_device_permission_status_not_supported"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{(( notificationAccessPermission != NewPermissionStatus.NotSupported) &amp;&amp; (safeUnbox(isThisDevice))) ? View.VISIBLE : View.GONE}"
                            android:text="@string/manage_device_permission_tap_top_open_settings"
                            android:textAppearance="?android:textAppearanceSmall"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <TextView
                            android:visibility="@{((safeUnbox(isThisDevice) || (notificationAccessPermission == NewPermissionStatus.NotSupported))) ? View.GONE : View.VISIBLE}"
                            android:text="@string/manage_device_permission_open_at_target_device"
                            android:textAppearance="?android:textAppearanceSmall"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

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

                <androidx.cardview.widget.CardView
                    android:visibility="@{BuildConfig.hasServer ? View.VISIBLE : View.GONE}"
                    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:textAppearance="?android:textAppearanceLarge"
                            android:text="@string/manage_device_network_time_verification_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

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

                        <RadioGroup
                            android:id="@+id/network_time_verification_radio_group"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                            <RadioButton
                                android:onClick="@{() -> handlers.changeNetworkTimeVerification(NetworkTime.Disabled) }"
                                android:checked="@{networkTimeVerification == NetworkTime.Disabled}"
                                android:text="@string/manage_device_network_time_disabled"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content" />

                            <RadioButton
                                android:onClick="@{() -> handlers.changeNetworkTimeVerification(NetworkTime.IfPossible) }"
                                android:checked="@{networkTimeVerification == NetworkTime.IfPossible}"
                                android:text="@string/manage_device_network_time_if_possible"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content" />

                            <RadioButton
                                android:onClick="@{() -> handlers.changeNetworkTimeVerification(NetworkTime.Enabled) }"
                                android:checked="@{networkTimeVerification == NetworkTime.Enabled}"
                                android:text="@string/manage_device_network_time_enabled"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content" />

                        </RadioGroup>

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

                <include android:id="@+id/device_reboot_manipulation"
                    layout="@layout/manage_device_reboot_manipulation_view" />

                <include android:visibility="@{BuildConfig.hasServer ? View.VISIBLE : View.GONE}"
                    android:id="@+id/send_device_connected"
                    layout="@layout/manage_send_device_connected" />

                <!-- this is always hidden in the offline version -->
                <include android:id="@+id/dont_ask_password_at_device"
                    layout="@layout/dont_ask_password_on_device_view" />

                <include android:id="@+id/troubleshooting_view"
                    layout="@layout/manage_device_troubleshooting_view" />

                <include
                    android:visibility="@{BuildConfig.hasServer ? View.VISIBLE : View.GONE}"
                    android:id="@+id/remove_device"
                    layout="@layout/remove_device_view" />

            </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>
