package org.keyscience.fragments;

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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import org.keyscience.R;
import org.keyscience.picasso.CircleTransform;

public class NewslettersFragment extends Fragment implements View.OnClickListener {

    ImageView newsletter1_1;
    ImageView newsletter1_2;
    ImageView newsletter1_3;
    ImageView newsletter2_1;
    ImageView newsletter2_2;
    ImageView newsletter2_3;
    ImageView newsletter2_4;
    ImageView newsletter3_1;
    ImageView newsletter4_1;
    ImageView newsletter4_2;
    ImageView newsletter5_1;
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.newsletters_fragment, container, false);

        newsletter1_1 = view.findViewById(R.id.newsletter1_1);
        newsletter1_2 = view.findViewById(R.id.newsletter1_2);
        newsletter1_3 = view.findViewById(R.id.newsletter1_3);
        newsletter2_1 = view.findViewById(R.id.newsletter2_1);
        newsletter2_2 = view.findViewById(R.id.newsletter2_2);
        newsletter2_3 = view.findViewById(R.id.newsletter2_3);
        newsletter2_4 = view.findViewById(R.id.newsletter2_4);
        newsletter3_1 = view.findViewById(R.id.newsletter3_1);
        newsletter4_1 = view.findViewById(R.id.newsletter4_1);
        newsletter4_2 = view.findViewById(R.id.newsletter4_2);
        newsletter5_1 = view.findViewById(R.id.newsletter5_1);

        newsletter1_1.setOnClickListener(this);
        newsletter1_2.setOnClickListener(this);
        newsletter1_3.setOnClickListener(this);
        newsletter2_1.setOnClickListener(this);
        newsletter2_2.setOnClickListener(this);
        newsletter2_3.setOnClickListener(this);
        newsletter2_4.setOnClickListener(this);
        newsletter3_1.setOnClickListener(this);
        newsletter4_1.setOnClickListener(this);
        newsletter4_2.setOnClickListener(this);
        newsletter5_1.setOnClickListener(this);

        loadCircleImages();

        return view;
    }

    public void loadCircleImages() {
        Picasso.get().load(R.drawable.newsletter_1_1).transform(new CircleTransform()).resize(200, 200).centerInside().noFade()
                .into(newsletter1_1);
        Picasso.get().load(R.drawable.newsletter_1_2).transform(new CircleTransform()).resize(200, 200).centerInside().noFade()
                .into(newsletter1_2);
        Picasso.get().load(R.drawable.newsletter_1_3).transform(new CircleTransform()).resize(200, 200).centerInside().noFade()
                .into(newsletter1_3);
        Picasso.get().load(R.drawable.newsletter_2_1).transform(new CircleTransform()).resize(200, 200).centerInside().noFade()
                .into(newsletter2_1);
        Picasso.get().load(R.drawable.newsletter_2_2).transform(new CircleTransform()).resize(200, 200).centerInside().noFade()
                .into(newsletter2_2);
        Picasso.get().load(R.drawable.newsletter_2_3).transform(new CircleTransform()).resize(200, 200).centerInside().noFade()
                .into(newsletter2_3);
        Picasso.get().load(R.drawable.newsletter_2_4).transform(new CircleTransform()).resize(200, 200).centerInside().noFade()
                .into(newsletter2_4);
        Picasso.get().load(R.drawable.newsletter_3_1).transform(new CircleTransform()).resize(200, 200).centerInside().noFade()
                .into(newsletter3_1);
        Picasso.get().load(R.drawable.newsletter_4_1).transform(new CircleTransform()).resize(200, 200).centerInside().noFade()
                .into(newsletter4_1);
        Picasso.get().load(R.drawable.newsletter_4_2).transform(new CircleTransform()).resize(200, 200).centerInside().noFade()
                .into(newsletter4_2);
        Picasso.get().load(R.drawable.newsletter_5_1).transform(new CircleTransform()).resize(200, 200).centerInside().noFade()
                .into(newsletter5_1);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.newsletter1_1:
                loadUrl("https://www.keyscience.org/wp-content/uploads/2014/01/CSPvol1.pdf");
                break;

            case R.id.newsletter1_2:
                loadUrl("https://www.keyscience.org/wp-content/uploads/2014/01/CSPvol2.pdf");
                break;

            case R.id.newsletter1_3:
                loadUrl("https://www.keyscience.org/wp-content/uploads/2014/01/ChiefScientistNewsletterVol1-3.pdf");
                break;

            case R.id.newsletter2_1:
                loadUrl("https://www.keyscience.org/wp-content/uploads/2014/01/ChiefScientistNewsletterVol2-1.pdf");
                break;

            case R.id.newsletter2_2:
                loadUrl("https://www.keyscience.org/wp-content/uploads/2014/01/ChiefScientistNewsletterVol2-3.pdf");
                break;

            case R.id.newsletter2_3:
                loadUrl("http://www.icontact-archive.com/Tw8sBwLUbjRh6Y2XjkLzRtuZnmSfpQ7z?w=3");
                break;

            case R.id.newsletter2_4:
                loadUrl("http://www.icontact-archive.com/Tw8sBwLUbjRh6Y2XjkLzRs0a08F2lM3w?w=3");
                break;

            case R.id.newsletter3_1:
                loadUrl("http://www.icontact-archive.com/Tw8sBwLUbjRh6Y2XjkLzRlcQwsdzCjsu?w=3");
                break;

            case R.id.newsletter4_1:
                loadUrl("http://www.icontact-archive.com/Tw8sBwLUbjRh6Y2XjkLzRhjLdvBz6Aqs?w=3");
                break;

            case R.id.newsletter4_2:
                loadUrl("http://www.icontact-archive.com/Tw8sBwLUbjRh6Y2XjkLzRnTw3ZhvZlJF?w=3");
                break;

            case R.id.newsletter5_1:
                loadUrl("http://www.icontact-archive.com/Tw8sBwLUbjRh6Y2XjkLzRk49Gu_U3VWH?w=3");
                break;

        }
    }

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