package org.keyscience.fragments;

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

import org.keyscience.R;

public class SocialMediaFragment extends Fragment implements View.OnClickListener {

    private static final String FACEBOOK_URL = "https://www.facebook.com/CitizenScientist";
    private static final String FACEBOOK_PROFILE = "CitizenScientist";
    private static final String TWIITER_PAGE = "https://twitter.com/KBCitSci";
    private static final String INSTAGRAM = "https://www.instagram.com/kbcitizenscience/";
    private static final String YOUTUBE = "https://www.youtube.com/channel/UCplpQu3cqOyS4Tylnk9llRQ";
    private static final String GOOGLE = "https://www.keyscience.org/";

    ImageView faceBook;
    ImageView twitter;
    ImageView google;
    ImageView youTube;
    ImageView instagram;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_socialmedia, container, false);
        faceBook = view.findViewById(R.id.facebookIcon);
        twitter = view.findViewById(R.id.twitterIcon);
        google = view.findViewById(R.id.googleIcon);
        youTube = view.findViewById(R.id.youtubeIcon);
        instagram = view.findViewById(R.id.instaIcon);

        // set onClickListeners for the icons
        faceBook.setOnClickListener(this);
        twitter.setOnClickListener(this);
        google.setOnClickListener(this);
        youTube.setOnClickListener(this);
        instagram.setOnClickListener(this);

        return view;
    }

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

            case R.id.facebookIcon:
                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getFacebookPageURL()));
                    if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
                        startActivity(intent);
                    }
                } catch (Exception e) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(FACEBOOK_URL)));
                }
                break;

            case R.id.twitterIcon:

                Intent twitterIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(TWIITER_PAGE));
                if (twitterIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                    startActivity(twitterIntent);
                }

                break;

            case R.id.googleIcon:

                Intent googeIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(GOOGLE));
                if (googeIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                    startActivity(googeIntent);
                }

                break;

            case R.id.youtubeIcon:

                Intent youtubeIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(YOUTUBE));
                if (youtubeIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                    startActivity(youtubeIntent);
                }

                break;

            case R.id.instaIcon:
                Intent instaIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(INSTAGRAM));
                if (instaIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                    startActivity(instaIntent);
                }

                break;
        }
    }


    /**
     * Check if facebook app is installed/enable, otherwise open
     * in browser
     *
     * @return facebook link
     */
    private String getFacebookPageURL() {
        try {
            PackageManager packageManager = getActivity().getPackageManager();
            int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;

            boolean activated = packageManager.getApplicationInfo("com.facebook.katana", 0).enabled;
            if (activated) {
                if ((versionCode >= 3002850)) {
                    return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
                } else {
                    return "fb://page/" + FACEBOOK_PROFILE;
                }
            } else {
                return FACEBOOK_URL;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return FACEBOOK_URL;
        }
    }

}
