package org.keyscience.fragments;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.keyscience.R;
import org.keyscience.adapters.ImageViewPagerAdapter;
import org.keyscience.app.WrapContentViewPager;

public class MiniChallengeFragment extends Fragment {

    private Context context;

    private Button keyRegistrationButton;
    private Button observationButton;

    private LinearLayout slideDotsPanel;
    private int dotsCount;
    private ImageView[] dots;

    private LinearLayout slideVimeoDotsPanel;
    private int VimeoDotsCount;
    private ImageView[] VimeoDots;

    WrapContentViewPager iNatViewPager;
    WrapContentViewPager vimeoViewPager;

    // If not hardcoded: Fragment MiniChallengeFragment{43b9b3e} not attached to a context.
    private int inat_slider_layout = R.layout.image_slider_layout;
    private Integer [] inat_images = {R.drawable.iphone_icon,R.drawable.search_icon,R.drawable.camera_icon};
    private String [] inat_imageTexts = {
            "1. DOWNLOAD THE iNATURALIST APP (IPHONE AND ANDROID), AND CREATE AN ACCOUNT.",
            "2. SEARCH iNATURALIST PROJECTS FOR “2017-18 MINI CHALLENGE #2” AND JOIN THE PROJECT.",
            "3. WHILE MAKING YOUR OBSERVATIONS, ADD THEM TO THE “2017-18 MINI CHALLENGE #2” PROJECT."};
    private String [] iNatUrls = { "https://play.google.com/store/apps/details?id=org.inaturalist.android", "", ""};

    private int vimeo_slider_layout = R.layout.image_slider_layout;
    private Integer [] vimeo_images = {R.drawable.vimeo_2,R.drawable.vimeo_1,R.drawable.vimeo_3};
    private String [] vimeo_imageTexts = {
            "MAKING AN OBSERVATION ON A MOBILE DEVICE",
            "ADDING AN OBSERVATION VIA THE WEB",
            "HOW TO TAKE IDENTIFIABLE PHOTOS"};
    private String [] vimeoURLs = { "https://vimeo.com/162581545", "https://vimeo.com/167431843", "https://vimeo.com/167341998"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {

        context = getContext();

        View view = inflater.inflate(R.layout.minichallenge_fragment, container, false);

        TextView mc_textView3 = view.findViewById(R.id.mc_textView3);
        mc_textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=org.inaturalist.android"));
                startActivity(browserIntent);
            }
        });

        // Vimeo ViewPager
        vimeoViewPager = view.findViewById(R.id.VimeoViewPager);
        vimeoViewPager.setOffscreenPageLimit(3);

        ImageViewPagerAdapter vimeoAdapter = new ImageViewPagerAdapter(context);
        vimeoAdapter.setImagesAndText(vimeo_slider_layout, vimeo_images, vimeo_imageTexts, vimeoURLs);

        vimeoViewPager.setAdapter(vimeoAdapter);

        // iNatViewpager
        iNatViewPager = view.findViewById(R.id.iNaturalistViewPager);
        iNatViewPager.setOffscreenPageLimit(3);

        ImageViewPagerAdapter iNatAdapter = new ImageViewPagerAdapter(context);
        iNatAdapter.setImagesAndText(inat_slider_layout, inat_images, inat_imageTexts, iNatUrls);

        iNatViewPager.setAdapter(iNatAdapter);

        // Dots Begin (iNatViewPager)
        slideDotsPanel = view.findViewById(R.id.SliderDots);
        dotsCount = iNatAdapter.getCount();
        dots = new ImageView[dotsCount];

        for( int i= 0; i < dotsCount; i++ ) {
            dots[i] = new ImageView(context);
            dots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.nonactive_dot));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            params.setMargins(8, 0, 8, 0);

            slideDotsPanel.addView(dots[i], params);

        }

        dots[0].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));

        iNatViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                for(int i = 0; i< dotsCount; i++){
                    dots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.nonactive_dot));
                }

                dots[position].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //Dots End

        // Dots Begin (Vimeo)
        slideVimeoDotsPanel = view.findViewById(R.id.VimeoSliderDots);
        VimeoDotsCount = vimeoAdapter.getCount();
        VimeoDots = new ImageView[VimeoDotsCount];

        for( int i= 0; i < VimeoDotsCount; i++ ) {
            VimeoDots[i] = new ImageView(context);
            VimeoDots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.nonactive_dot));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            params.setMargins(8, 0, 8, 0);

            slideVimeoDotsPanel.addView(VimeoDots[i], params);

        }

        VimeoDots[0].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));

        vimeoViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                for(int i = 0; i< VimeoDotsCount; i++){
                    VimeoDots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.nonactive_dot));
                }

                VimeoDots[position].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //Dots End

        // Paragraphs that require HTML
        String minichallenge_rules = getResources().getString(R.string.mini_challenge_rules);
        TextView mc_textView10 = view.findViewById(R.id.mc_minichallenge_rulesText);
        mc_textView10.setText(Html.fromHtml(minichallenge_rules));

        initButtons(view);

        return view;
    }

    private void initButtons(View v) {
        keyRegistrationButton = v.findViewById(R.id.mc_registerOnline);
        keyRegistrationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openRegistrationUrl(v);
            }
        });

        observationButton = v.findViewById(R.id.observationButton);
        observationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openMakeObservationUrl(v);
            }
        });
    }

    public void openRegistrationUrl(View view) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.keyscience.org/projects/key-challenge/mini/"));
        startActivity(browserIntent);
    }

    public void openMakeObservationUrl(View view) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.inaturalist.org/projects/2017-18-mini-challenge-2"));
        startActivity(browserIntent);
    }
}
