/*
 * Copyright 2018-2022 Pranav Pandey
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pranavpandey.android.dynamic.support.tutorial.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.core.widget.NestedScrollView;

import com.pranavpandey.android.dynamic.support.Dynamic;
import com.pranavpandey.android.dynamic.support.R;
import com.pranavpandey.android.dynamic.support.fragment.DynamicFragment;
import com.pranavpandey.android.dynamic.support.theme.DynamicTheme;
import com.pranavpandey.android.dynamic.support.tutorial.DynamicTutorial;
import com.pranavpandey.android.dynamic.support.tutorial.Tutorial;
import com.pranavpandey.android.dynamic.support.tutorial.activity.DynamicTutorialActivity;
import com.pranavpandey.android.dynamic.support.util.DynamicResourceUtils;
import com.pranavpandey.android.dynamic.theme.Theme;
import com.pranavpandey.android.dynamic.util.DynamicColorUtils;

/**
 * A {@link DynamicTutorialFragment} with an image, title, subtitle and description that
 * will be used with the {@link DynamicTutorialActivity}.
 */
public class DynamicTutorialFragment extends DynamicFragment
        implements Tutorial<DynamicTutorial, DynamicTutorialFragment> {

    /**
     * Fragment argument key to set the dynamic tutorial.
     */
    public static final String ADS_ARGS_TUTORIAL = "ads_args_tutorial";

    /**
     * Tutorial key to maintain its state.
     */
    public static final String ADS_STATE_TUTORIAL = "ads_state_tutorial";

    /**
     * Dynamic tutorial used by this fragment.
     */
    private DynamicTutorial mDynamicTutorial;

    /**
     * Root view of this fragment.
     */
    private ViewGroup mRootView;

    /**
     * Image view to show the tutorial image.
     */
    private ImageView mImageView;

    /**
     * Scroll view to show the scrolling content.
     */
    private NestedScrollView mScrollView;

    /**
     * Card view of this fragment.
     */
    private CardView mCardView;

    /**
     * Text view to show the tutorial title.
     */
    private TextView mTitleView;

    /**
     * Text view to show the tutorial subtitle.
     */
    private TextView mSubtitleView;

    /**
     * Text view to show the tutorial description.
     */
    private TextView mDescriptionView;

    /**
     * Function to initialize this fragment.
     *
     * @param dynamicTutorial The dynamic tutorial for this fragment.
     *
     * @return An instance of {@link DynamicTutorialFragment}.
     */
    public static @NonNull DynamicTutorialFragment newInstance(
            @NonNull DynamicTutorial dynamicTutorial) {
        DynamicTutorialFragment fragment = new DynamicTutorialFragment();
        Bundle args = new Bundle();
        args.putParcelable(ADS_ARGS_TUTORIAL, dynamicTutorial);
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            if (requireArguments().containsKey(ADS_ARGS_TUTORIAL)) {
                mDynamicTutorial = requireArguments().getParcelable(ADS_ARGS_TUTORIAL);
            }
        }

        if (getSavedInstanceState() != null) {
            mDynamicTutorial = getSavedInstanceState().getParcelable(ADS_STATE_TUTORIAL);
        }
    }

    @Override
    public @Nullable View onCreateView(@NonNull LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ads_fragment_tutorial_simple, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mRootView = view.findViewById(R.id.ads_tutorial_simple);
        mImageView = view.findViewById(R.id.ads_tutorial_simple_image);
        mScrollView = view.findViewById(R.id.ads_tutorial_simple_scroller);
        mCardView = view.findViewById(R.id.ads_tutorial_simple_card);
        mTitleView = view.findViewById(R.id.ads_tutorial_simple_title);
        mSubtitleView = view.findViewById(R.id.ads_tutorial_simple_subtitle);
        mDescriptionView = view.findViewById(R.id.ads_tutorial_simple_description);

        if (mDynamicTutorial.isSharedElement()) {
            Dynamic.setTransitionName(mRootView, Tutorial.ADS_NAME_TUTORIAL);
            Dynamic.setTransitionName(mImageView, Tutorial.ADS_NAME_TUTORIAL_IMAGE);
            Dynamic.setTransitionName(mTitleView, Tutorial.ADS_NAME_TUTORIAL_TITLE);
            Dynamic.setTransitionName(mSubtitleView, Tutorial.ADS_NAME_TUTORIAL_SUBTITLE);
        } else {
            Dynamic.setTransitionName(mRootView, null);
            Dynamic.setTransitionName(mImageView, null);
            Dynamic.setTransitionName(mTitleView, null);
            Dynamic.setTransitionName(mSubtitleView, null);
        }

        if (mDynamicTutorial != null) {
            if (mImageView != null) {
                Dynamic.set(mImageView, DynamicResourceUtils.getDrawable(
                        requireContext(), mDynamicTutorial.getImageRes()));
            }

            Dynamic.set(mTitleView, mDynamicTutorial.getTitle());
            Dynamic.set(mSubtitleView, mDynamicTutorial.getSubtitle());
            Dynamic.set(mDescriptionView, mDynamicTutorial.getDescription());
        }

        tintWidgets(getColor(), getTintColor());
    }

    @Override
    public @Nullable Object getDynamicEnterTransition() {
        return null;
    }

    @Override
    public @Nullable Object getDynamicExitTransition() {
        return null;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putParcelable(ADS_STATE_TUTORIAL, mDynamicTutorial);
    }

    /**
     * Tint the widgets according to the supplied colors.
     *
     * @param color The color to be applied.
     * @param tintColor The tint color to be applied.
     */
    private void tintWidgets(@ColorInt int color, @ColorInt int tintColor) {
        if (mDynamicTutorial != null && mDynamicTutorial.isTintImage()) {
            Dynamic.tint(mImageView, tintColor, color);
        } else {
            Dynamic.setColorType(mImageView, Theme.ColorType.NONE);
        }

        Dynamic.setContrastWithColor(mCardView, color);
        Dynamic.setForceElevation(mCardView,
                !DynamicTheme.getInstance().get().isBackgroundSurface());
        if (Dynamic.isStroke()) {
            Dynamic.setColor(mCardView, DynamicColorUtils.setAlpha(color,
                    Color.alpha(DynamicTheme.getInstance().get().getSurfaceColor())));
        } else {
            Dynamic.setColor(mCardView, DynamicTheme.getInstance().get().isBackgroundSurface()
                    ? color : DynamicTheme.getInstance().generateSurfaceColor(color));
        }

        Dynamic.setContrastWithColor(mScrollView, color);
        Dynamic.setContrastWithColor(mTitleView, Dynamic.getColor(mCardView, color));
        Dynamic.setContrastWithColor(mSubtitleView, Dynamic.getColor(mCardView, color));
        Dynamic.setContrastWithColor(mDescriptionView, Dynamic.getColor(mCardView, color));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

    @Override
    public void onPageSelected(int position) {
        onColorChanged(getColor(), getTintColor());
    }

    @Override
    public void onPageScrollStateChanged(int state) { }

    @Override
    public int getTutorialId() {
        return mDynamicTutorial.getTutorialId();
    }

    @Override
    public @NonNull DynamicTutorial getTutorial() {
        return mDynamicTutorial;
    }

    @Override
    public @NonNull DynamicTutorialFragment createTutorial() {
        return this;
    }

    @Override
    public @ColorInt int getColor() {
        return mDynamicTutorial != null ? mDynamicTutorial.getColor()
                : DynamicTheme.getInstance().get().getPrimaryColor();
    }

    @Override
    public @ColorInt int getTintColor() {
        return mDynamicTutorial != null ? mDynamicTutorial.getTintColor()
                : DynamicTheme.getInstance().get().getTintPrimaryColor();
    }

    @Override
    public void onColorChanged(@ColorInt int color, @ColorInt int tintColor) {
        tintWidgets(color, tintColor);
    }

    @Override
    public void onSetPadding(int left, int top, int right, int bottom) {
        if (mRootView == null || bottom <= 0 || mRootView.getPaddingBottom() >= bottom) {
            return;
        }

        mRootView.setPadding(mRootView.getPaddingLeft() + left,
                mRootView.getPaddingTop() + top,
                mRootView.getPaddingRight() + right,
                mRootView.getPaddingBottom() + bottom);
    }

    /**
     * Returns the root view of this fragment.
     *
     * @return The root view of this fragment.
     */
    public @NonNull ViewGroup getRootView() {
        return mRootView;
    }

    /**
     * Returns the image view to show the tutorial image.
     *
     * @return The image view to show the tutorial image.
     */
    public @NonNull ImageView getImageView() {
        return mImageView;
    }

    /**
     * Returns the scroll view to show the scrolling content.
     *
     * @return The scroll view to show the scrolling content.
     */
    public @NonNull NestedScrollView getScrollView() {
        return mScrollView;
    }

    /**
     * Returns the card view of this fragment.
     *
     * @return The card view of this fragment.
     */
    public @NonNull CardView getCardView() {
        return mCardView;
    }

    /**
     * Returns the text view to show the tutorial title.
     *
     * @return The text view to show the tutorial title.
     */
    public @NonNull TextView getTitleView() {
        return mTitleView;
    }

    /**
     * Returns the text view to show the tutorial subtitle.
     *
     * @return The text view to show the tutorial subtitle.
     */
    public @NonNull TextView getSubtitleView() {
        return mSubtitleView;
    }

    /**
     * Returns the text view to show the tutorial description.
     *
     * @return The text view to show the tutorial description.
     */
    public @NonNull TextView getDescriptionView() {
        return mDescriptionView;
    }
}
