package org.keyscience.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.keyscience.R;
import org.keyscience.adapters.LabPagerAdapter;
import org.keyscience.adapters.OurPartnersAdapter;
import org.keyscience.app.Constants;
import org.keyscience.app.WrapContentViewPager;
import org.keyscience.models.OurPartnersModel;

import java.util.List;

public class AboutUsFragment extends Fragment {

    private int lab_slider_layout = R.layout.lecture_series_item; // reuse the lecture series item layout for this slider


    private WrapContentViewPager labViewPager;
    private LinearLayout slideLabDotsPanel;
    private int labDotsCount;
    private ImageView[] labDots;


    private WrapContentViewPager programViewPager;
    private LinearLayout slideProgramDotsPanel;
    private int programDotsCount;
    private ImageView[] programDots;

    private ImageView kbcImage;
    private ImageView villageImage;
    private ImageView umImage;
    private ImageView knightImage;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.aboutus_fragment, container, false);

        RecyclerView recyclerViewOne;

        recyclerViewOne = view.findViewById(R.id.our_partners_recyclerOne);

        // setup first recycler
        setUpRecyclerView(recyclerViewOne, view, OurPartnersModel.getDataOnlyFour(getContext()));

        // content for lab slider
        String[] lab_titles = {getString(R.string.LEARN), getString(R.string.EXPLORE_KEY), getString(R.string.RECORD), getString(R.string.REVIEW)};
        String[] lab_subtitles = {getString(R.string.LEARN_SUB), getString(R.string.EXPLORE_KEY_SUB), getString(R.string.RECORD_SUB), getString(R.string.REVIEW_SUB)};
        int[] lab_images = {R.drawable.lab_learn, R.drawable.lab_explore, R.drawable.lab_record, R.drawable.lab_review};

        TextView overViewText = view.findViewById(R.id.au_overview);
        overViewText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.WEB_ADDRESS));
                if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
                    startActivity(intent);
                }
            }
        });

        // setup lab slider
        labViewPager = view.findViewById(R.id.AboutUsViewPager);
        labViewPager.setOffscreenPageLimit(lab_titles.length);  // This should be the number of frames that exists

        LabPagerAdapter labAdapter = new LabPagerAdapter(getContext(), lab_slider_layout, lab_images, lab_titles, lab_subtitles);
        labViewPager.setAdapter(labAdapter);

        // Dots Begin (lab)
        slideLabDotsPanel = view.findViewById(R.id.LabSliderDots);
        labDotsCount = labAdapter.getCount();
        labDots = new ImageView[labDotsCount];
        for (int i = 0; i < labDotsCount; i++) {
            labDots[i] = new ImageView(getContext());
            labDots[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);

            slideLabDotsPanel.addView(labDots[i], params);

        }
        labDots[0].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));
        labViewPager.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 < labDotsCount; i++) {
                    labDots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.nonactive_dot));
                }
                labDots[position].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        //Dots End


        // setup programs slider
        // TODO add Field activities once the fragment is implemented
        String[] program_titles = {Constants.KEY_CHALLENGE, Constants.REEF_RESTORATION, Constants.WATER_WATCH,
                Constants.SEA_LEVEL_RISE, Constants.LECTURE_SERIES};

        String[] program_subtitles = {getString(R.string.programSliderKC), getString(R.string.programSliderRF), getString(R.string.programSliderWW), getString(R.string.programSliderSLR), getString(R.string.programSliderLS)};

        int[] program_images = {R.drawable.key_challenge_home, R.drawable.reef_restoration_home, R.drawable.water_watch_home,
                R.drawable.sea_level_home, R.drawable.lectures_home};

        // setup program slider
        programViewPager = view.findViewById(R.id.programsViewPager);
        programViewPager.setOffscreenPageLimit(program_titles.length);
        LabPagerAdapter programAdapter = new LabPagerAdapter(getContext(), R.layout.program_list_item, program_images, program_titles, program_subtitles);
        programViewPager.setAdapter(programAdapter);

        // Dots Begin (program)
        slideProgramDotsPanel = view.findViewById(R.id.programsSliderDots);
        programDotsCount = programAdapter.getCount();
        programDots = new ImageView[programDotsCount];
        for (int i = 0; i < programDotsCount; i++) {
            programDots[i] = new ImageView(getContext());
            programDots[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);

            slideProgramDotsPanel.addView(programDots[i], params);

        }
        programDots[0].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));
        programViewPager.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 < programDotsCount; i++) {
                    programDots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.nonactive_dot));
                }
                programDots[position].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        //Dots End


//        // set up image links
//        kbcImage = view.findViewById(R.id.kbcfImage);
//        villageImage = view.findViewById(R.id.villageImage);
//        umImage = view.findViewById(R.id.umImage);
//        knightImage = view.findViewById(R.id.knightImage);
//
//        kbcImage.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                launchActivity(getContext().getString(R.string.KBCF));
//            }
//        });
//        villageImage.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                launchActivity(getContext().getString(R.string.VILLAGE));
//            }
//        });
//        umImage.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                launchActivity(getContext().getString(R.string.UM));
//            }
//        });
//        knightImage.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                launchActivity(getContext().getString(R.string.KNIGHT));
//            }
//        });


        return view;
    }

    private void launchActivity(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    private void setUpRecyclerView(RecyclerView recyclerView, View view, List<OurPartnersModel> data) {
        recyclerView.setFocusable(false);
        OurPartnersAdapter adapter = new OurPartnersAdapter(data, view.getContext());
        recyclerView.setAdapter(adapter);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(view.getContext(), 2); // (Context context, int spanCount)
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(gridLayoutManager);

        //evenly distribute column
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view); // item position
                int spanCount = 2;
                int spacing = 15;//spacing between views in grid

                if (position >= 0) {
                    int column = position % spanCount; // item column

                    outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                    outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                    if (position < spanCount) { // top edge
                        outRect.top = spacing;
                    }
                    outRect.bottom = spacing; // item bottom
                } else {
                    outRect.left = 0;
                    outRect.right = 0;
                    outRect.top = 0;
                    outRect.bottom = 0;
                }
            }
        });
    }
}
