<?xml version="1.0" encoding="utf-8"?>
<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">

    <data>

        <import type="com.topjohnwu.magisk.base.viewmodel.LoadingViewModel.State" />

        <import type="com.topjohnwu.magisk.ui.home.SafetyNetState" />

        <import type="com.topjohnwu.magisk.ui.home.MagiskState" />

        <import type="com.topjohnwu.magisk.ui.home.MagiskItem" />

        <import type="com.topjohnwu.magisk.R" />

        <variable
            name="viewModel"
            type="com.topjohnwu.magisk.ui.home.HomeViewModel" />

    </data>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        app:onRefreshListener="@{() -> viewModel.refresh()}"
        app:refreshing="@{viewModel.loading}">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/linearLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="@dimen/margin_generic">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:id="@+id/icon"
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_marginEnd="16dp"
                        app:layout_constraintBottom_toBottomOf="@+id/app_name"
                        app:layout_constraintEnd_toStartOf="@+id/app_name"
                        app:layout_constraintHorizontal_bias="0.42"
                        app:layout_constraintHorizontal_chainStyle="packed"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="@+id/app_name"
                        app:srcCompat="@drawable/ic_logo" />

                    <TextView
                        android:id="@+id/app_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:paddingTop="10dp"
                        android:paddingBottom="10dp"
                        android:text="@string/app_name"
                        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="0.5"
                        app:layout_constraintStart_toEndOf="@+id/icon"
                        app:layout_constraintTop_toTopOf="parent" />

                </androidx.constraintlayout.widget.ConstraintLayout>

                <View
                    style="@style/Widget.Divider.Horizontal"
                    android:layout_width="match_parent"
                    android:layout_margin="@dimen/margin_generic" />

                <include
                    android:id="@+id/home_magisk_version"
                    additionalInfo="@{viewModel.magiskAdditionalInfo}"
                    currentVersion="@{viewModel.magiskCurrentVersion}"
                    item="@{MagiskItem.MAGISK}"
                    latestVersion="@{viewModel.magiskLatestVersion}"
                    layout="@layout/include_update_card"
                    state="@{viewModel.magiskState}"
                    text="@{viewModel.magiskStateText}"
                    viewModel="@{viewModel}" />

                <include
                    android:id="@+id/home_manager_version"
                    additionalInfo="@{viewModel.managerAdditionalInfo}"
                    currentVersion="@{viewModel.managerCurrentVersion}"
                    item="@{MagiskItem.MANAGER}"
                    latestVersion="@{viewModel.managerLatestVersion}"
                    layout="@layout/include_update_card"
                    state="@{viewModel.managerState}"
                    text="@{viewModel.managerStateText}"
                    viewModel="@{viewModel}"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/margin_generic" />

                <View
                    style="@style/Widget.Divider.Horizontal"
                    gone="@{!viewModel.isConnected}"
                    android:layout_width="match_parent"
                    android:layout_margin="@dimen/margin_generic" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:animateLayoutChanges="true"
                    gone="@{!viewModel.isConnected}"
                    android:onClick="@{() -> viewModel.advancedPressed()}"
                    android:orientation="vertical">

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="?attr/selectableItemBackground"
                        android:minHeight="48dp">

                        <androidx.appcompat.widget.AppCompatImageView
                            android:id="@+id/arrow"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="16dp"
                            android:rotation="@{viewModel.isAdvancedExpanded ? 180 : 0}"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintStart_toEndOf="@+id/title"
                            app:layout_constraintTop_toTopOf="parent"
                            app:srcCompat="@drawable/ic_arrow"
                            app:tint="?attr/imageColorTint" />

                        <TextView
                            android:id="@+id/title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:text="@string/advanced_settings_title"
                            android:textStyle="bold"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />

                    </androidx.constraintlayout.widget.ConstraintLayout>

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:id="@+id/install_option_expand"
                        gone="@{!viewModel.isAdvancedExpanded}"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <CheckBox
                            android:id="@+id/keep_force_enc"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:checked="@={viewModel.isForceEncryption}"
                            android:text="@string/keep_force_encryption"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintRight_toRightOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintWidth_default="wrap"
                            app:layout_constraintWidth_min="300dp" />

                        <CheckBox
                            android:id="@+id/keep_verity"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:checked="@={viewModel.isKeepVerity}"
                            android:text="@string/keep_dm_verity"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintRight_toRightOf="parent"
                            app:layout_constraintTop_toBottomOf="@+id/keep_force_enc"
                            app:layout_constraintWidth_default="wrap"
                            app:layout_constraintWidth_min="300dp" />

                        <CheckBox
                            android:id="@+id/recovery_mode"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:checked="@={viewModel.isRecovery}"
                            android:text="@string/recovery_mode"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintRight_toRightOf="parent"
                            app:layout_constraintTop_toBottomOf="@+id/keep_verity"
                            app:layout_constraintWidth_default="wrap"
                            app:layout_constraintWidth_min="300dp" />

                    </androidx.constraintlayout.widget.ConstraintLayout>

                </LinearLayout>

                <View
                    style="@style/Widget.Divider.Horizontal"
                    gone="@{!viewModel.isActive || !viewModel.isConnected}"
                    android:layout_width="match_parent"
                    android:layout_margin="@dimen/margin_generic" />

                <LinearLayout
                    gone="@{!viewModel.isConnected || !viewModel.hasGMS}"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="@dimen/margin_generic_half"
                        android:paddingBottom="@dimen/margin_generic_half">

                        <ImageView
                            android:id="@+id/sn_logo"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:tint="@color/green500"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toStartOf="@+id/safetyNet_status"
                            app:layout_constraintHorizontal_chainStyle="packed"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:srcCompat="@drawable/ic_safetynet" />

                        <TextView
                            android:id="@+id/safetyNet_status"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="@dimen/margin_generic"
                            android:layout_marginRight="@dimen/margin_generic"
                            android:gravity="center"
                            android:maxLines="1"
                            android:text="@{viewModel.safetyNetTitle}"
                            android:textStyle="bold"
                            app:autoSizeMaxTextSize="14sp"
                            app:autoSizeTextType="uniform"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toStartOf="@+id/safetyNet_refresh"
                            app:layout_constraintStart_toEndOf="@+id/sn_logo"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintWidth_min="200dp" />

                        <ImageView
                            android:id="@+id/safetyNet_refresh"
                            invisible="@{viewModel.ctsState == SafetyNetState.LOADING || viewModel.basicIntegrityState == SafetyNetState.LOADING}"
                            android:layout_width="25dp"
                            android:layout_height="25dp"
                            android:onClick="@{() -> viewModel.safetyNetPressed()}"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toEndOf="@+id/safetyNet_status"
                            app:layout_constraintTop_toTopOf="parent"
                            app:srcCompat="@drawable/ic_refresh" />

                        <ProgressBar
                            android:id="@+id/safetyNet_check_progress"
                            style="@style/Widget.Progress"
                            goneUnless="@{viewModel.ctsState == SafetyNetState.LOADING || viewModel.basicIntegrityState == SafetyNetState.LOADING}"
                            android:layout_width="0dp"
                            android:layout_height="0dp"
                            app:layout_constraintBottom_toBottomOf="@+id/safetyNet_refresh"
                            app:layout_constraintEnd_toEndOf="@+id/safetyNet_refresh"
                            app:layout_constraintStart_toStartOf="@+id/safetyNet_refresh"
                            app:layout_constraintTop_toTopOf="@+id/safetyNet_refresh" />

                    </androidx.constraintlayout.widget.ConstraintLayout>

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:id="@+id/safetyNet_expand"
                        gone="@{viewModel.safetyNetState == State.LOADING}"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingBottom="10dp">

                        <androidx.appcompat.widget.AppCompatImageView
                            android:id="@+id/cts_status_icon"
                            srcCompat="@{viewModel.ctsState == SafetyNetState.PASS ? R.drawable.ic_check_circle : R.drawable.ic_cancel}"
                            android:layout_width="25dp"
                            android:layout_height="25dp"
                            android:layout_marginEnd="8dp"
                            app:layout_constraintBottom_toBottomOf="@+id/cts_status"
                            app:layout_constraintEnd_toStartOf="@+id/cts_status"
                            app:layout_constraintTop_toTopOf="@+id/cts_status"
                            app:tint="@{viewModel.ctsState == SafetyNetState.PASS ? @color/colorCorrect : @color/colorError}"
                            tools:srcCompat="@drawable/ic_check_circle" />

                        <androidx.appcompat.widget.AppCompatImageView
                            android:id="@+id/basic_status_icon"
                            srcCompat="@{viewModel.basicIntegrityState == SafetyNetState.PASS ? R.drawable.ic_check_circle : R.drawable.ic_cancel}"
                            android:layout_width="25dp"
                            android:layout_height="25dp"
                            android:layout_marginEnd="8dp"
                            app:layout_constraintBottom_toBottomOf="@+id/basic_status"
                            app:layout_constraintEnd_toStartOf="@+id/basic_status"
                            app:layout_constraintHorizontal_bias="0.5"
                            app:layout_constraintHorizontal_chainStyle="packed"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="@+id/basic_status"
                            app:tint="@{viewModel.basicIntegrityState == SafetyNetState.PASS ? @color/colorCorrect : @color/colorError}"
                            tools:srcCompat="@drawable/ic_check_circle" />

                        <TextView
                            android:id="@+id/cts_status"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:padding="6dp"
                            android:text="@{String.format(`ctsProfile: %b`, viewModel.ctsState == SafetyNetState.PASS)}"
                            android:textStyle="bold"
                            app:layout_constraintEnd_toEndOf="@+id/basic_status"
                            app:layout_constraintStart_toStartOf="@+id/basic_status"
                            app:layout_constraintTop_toTopOf="parent"
                            tools:text="ctsProfile: true" />

                        <TextView
                            android:id="@+id/basic_status"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:padding="6dp"
                            android:text="@{String.format(`basicIntegrity: %b`, viewModel.basicIntegrityState == SafetyNetState.PASS)}"
                            android:textStyle="bold"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintHorizontal_bias="0.5"
                            app:layout_constraintStart_toEndOf="@+id/basic_status_icon"
                            app:layout_constraintTop_toBottomOf="@+id/cts_status"
                            tools:text="basicIntegrity: true" />


                    </androidx.constraintlayout.widget.ConstraintLayout>

                </LinearLayout>

                <View
                    style="@style/Widget.Divider.Horizontal"
                    android:layout_width="match_parent"
                    android:layout_margin="@dimen/margin_generic" />

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <FrameLayout
                        android:id="@+id/twitter"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:clickable="true"
                        android:focusable="true"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:onClick="@{() -> viewModel.twitterPressed()}"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toStartOf="@+id/github"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/paypal">

                        <ImageView
                            android:layout_width="35dp"
                            android:layout_height="35dp"
                            android:layout_gravity="center_horizontal"
                            android:layout_margin="10dp"
                            android:tint="?attr/imageColorTint"
                            app:srcCompat="@drawable/ic_twitter" />

                    </FrameLayout>

                    <FrameLayout
                        android:id="@+id/github"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:clickable="true"
                        android:focusable="true"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:onClick="@{() -> viewModel.githubPressed()}"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toStartOf="@+id/xda"
                        app:layout_constraintStart_toEndOf="@+id/twitter">

                        <ImageView
                            android:layout_width="35dp"
                            android:layout_height="35dp"
                            android:layout_gravity="center_horizontal"
                            android:layout_margin="10dp"
                            android:tint="?attr/imageColorTint"
                            app:srcCompat="@drawable/ic_github" />

                    </FrameLayout>

                    <FrameLayout
                        android:id="@+id/xda"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:clickable="true"
                        android:focusable="true"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:onClick="@{() -> viewModel.xdaPressed()}"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toEndOf="@id/github">

                        <ImageView
                            android:layout_width="35dp"
                            android:layout_height="35dp"
                            android:layout_gravity="center_horizontal"
                            android:layout_margin="10dp"
                            android:tint="?attr/imageColorTint"
                            app:srcCompat="@drawable/ic_xda" />

                    </FrameLayout>

                    <LinearLayout
                        android:id="@+id/patreon"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:background="?android:attr/selectableItemBackground"
                        android:clickable="true"
                        android:focusable="true"
                        android:gravity="center_horizontal"
                        android:onClick="@{() -> viewModel.patreonPressed()}"
                        android:orientation="horizontal"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toEndOf="@+id/paypal"
                        app:layout_constraintTop_toTopOf="parent">

                        <ImageView
                            android:layout_width="35dp"
                            android:layout_height="35dp"
                            android:layout_gravity="center"
                            android:layout_margin="10dp"
                            android:tint="?attr/imageColorTint"
                            app:srcCompat="@drawable/ic_patreon" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_margin="5dp"
                            android:text="Patreon"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textStyle="bold" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/paypal"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:background="?android:attr/selectableItemBackground"
                        android:clickable="true"
                        android:focusable="true"
                        android:gravity="center_horizontal"
                        android:onClick="@{() -> viewModel.paypalPressed()}"
                        android:orientation="horizontal"
                        app:layout_constraintEnd_toStartOf="@+id/patreon"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent">

                        <ImageView
                            android:layout_width="35dp"
                            android:layout_height="35dp"
                            android:layout_gravity="center"
                            android:layout_margin="10dp"
                            android:tint="?attr/imageColorTint"
                            app:srcCompat="@drawable/ic_paypal" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_margin="5dp"
                            android:text="PayPal"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textStyle="bold" />
                    </LinearLayout>

                </androidx.constraintlayout.widget.ConstraintLayout>

                <View
                    style="@style/Widget.Divider.Horizontal"
                    gone="@{!viewModel.isActive}"
                    android:layout_width="match_parent"
                    android:layout_margin="@dimen/margin_generic" />

                <com.google.android.material.button.MaterialButton
                    style="@style/Widget.Button.Text"
                    gone="@{!viewModel.isActive}"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:onClick="@{() -> viewModel.uninstallPressed()}"
                    android:text="@string/uninstall" />

            </LinearLayout>

        </ScrollView>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</layout>
