package com.hikapro.backpack.presenter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareHashtag;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.widget.ShareDialog;
import com.hikapro.backpack.R;
import com.hikapro.backpack.model.Model;
import com.hikapro.backpack.view.View;

import java.lang.ref.WeakReference;

/**
 * Created by tariel on 16/05/16.
 */
public class SharePresenter implements Presenter.Share {

    private ShareDialog shareDialog;
    private CallbackManager callbackManager;

    private Button facebookButton;
    private Button twitterButton;

    private WeakReference<View.Share>   view;
    private Model.Share                 model;

    @Override
    public Context getAppContext() {
        try {
            return getView().getAppContext();
        } catch (NullPointerException e) {
            return null;
        }
    }

    @Override
    public Context getActivityContext() {
        try {
            return getView().getActivityContext();
        } catch (NullPointerException e) {
            return null;
        }
    }

    @Override
    public void setView(View.Share view) {
        this.view = new WeakReference<>(view);

    }

    @Override
    public void setModel(Model.Share model) {
        this.model = model;
    }

    @Override
    public Model.Share getModel() {
        return model;
    }

    @Override
    public void onDestroy(boolean isChangingConfiguration) {
        view = null;
        model.onDestroy(isChangingConfiguration);
        if ( !isChangingConfiguration ) {
            model = null;
        }
    }

    @Override
    public android.view.View onCreateView(LayoutInflater inflater, ViewGroup container,
                                          Bundle savedInstanceState) {
        android.view.View v = inflater.inflate(R.layout.fragment_share, container, false);

        FacebookSdk.sdkInitialize(getAppContext());
        //AppEventsLogger.activateApp(getActivityContext());
        callbackManager = CallbackManager.Factory.create();
        shareDialog = new ShareDialog((Activity) getActivityContext());
        facebookButton = (Button) v.findViewById(R.id.facebook_share_button);
        twitterButton = (Button) v.findViewById(R.id.twitter_share_button);
        shareDialog.registerCallback(callbackManager, new

                FacebookCallback<Sharer.Result>() {
                    @Override
                    public void onSuccess(Sharer.Result result) {}

                    @Override
                    public void onCancel() {}

                    @Override
                    public void onError(FacebookException error) {}
                });
        facebookButton.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(android.view.View v) {
                if (ShareDialog.canShow(ShareLinkContent.class)) {


                    ShareLinkContent linkContent = new ShareLinkContent.Builder()
                            .setContentTitle("Test Pack With Me app")
                            .setContentDescription("Test of facebook integration")
                            .setContentUrl(Uri.parse("http://developers.facebook.com/android"))
                            .setShareHashtag(new ShareHashtag.Builder()
                                    .setHashtag("#PackWithMe")
                                    .build())
                            .build();

                    shareDialog.show(linkContent);
                }
            }
        });
        twitterButton.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(android.view.View v) {
                Toast.makeText(getActivityContext(), "Not implemented!", Toast.LENGTH_SHORT).show();
            }
        });

        return v;
    }

    private View.Share getView() throws NullPointerException {
        if ( view != null )
            return view.get();
        else
            throw new NullPointerException("View is unavailable");
    }
}
