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

import org.keyscience.R;

public class WaterQualityFragment extends Fragment {
    private static final String KBCF = "http://www.kbcf.org";
    private static final String VILLAGE_KV = "http://keybiscayne.fl.gov";
    private static final String WATERKEEPERS = "https://www.miamiwaterkeeper.org";
    private static final String WATERQUALITY = "https://www.keyscience.org/projects/key-biscayne-water-quality/#testresults";


    private Context context;

    private Button waterQualityButton;
    private ImageView kbcfImage;
    private ImageView villageImage;
    private ImageView waterKeepersImage;

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

        // set up image links
        kbcfImage = view.findViewById(R.id.kbcfImage);
        villageImage = view.findViewById(R.id.villageImage);
        waterKeepersImage = view.findViewById(R.id.waterKeepersLogo);

        kbcfImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launchActivity(KBCF);
            }
        });
        villageImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launchActivity(VILLAGE_KV);
            }
        });
        waterKeepersImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launchActivity(WATERKEEPERS);
            }
        });

        initButtons(view);

        return view;
    }

    public void initButtons(View view) {
        waterQualityButton = view.findViewById(R.id.waterquality_button);
        waterQualityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launchActivity(WATERQUALITY);
            }
        });
    }

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