
package org.zarroboogs.weibo.dialogfragment;

import org.zarroboogs.weibo.BeeboApplication;
import org.zarroboogs.weibo.R;
import org.zarroboogs.weibo.activity.UserInfoActivity;
import org.zarroboogs.weibo.support.utils.Utility;
import org.zarroboogs.weibo.support.utils.WebBrowserSelector;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Browser;
import android.support.v4.app.DialogFragment;
import android.widget.Toast;


@SuppressLint("ValidFragment")
public class LongClickLinkDialog extends DialogFragment {

    private Uri uri;

    public LongClickLinkDialog() {

    }

    public LongClickLinkDialog(Uri uri) {
        this.uri = uri;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("uri", uri);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null)
            this.uri = savedInstanceState.getParcelable("uri");
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        CharSequence[] strangerItems = {
                getString(R.string.open), getString(R.string.copy)
        };

        builder.setTitle(getStringContent()).setItems(strangerItems, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        Context context = getActivity();
                        if (uri.getScheme().startsWith("http")) {
                            String url = uri.toString();
                            if (Utility.isWeiboAccountIdLink(url)) {
                                Intent intent = new Intent(context, UserInfoActivity.class);
                                intent.putExtra("id", Utility.getIdFromWeiboAccountLink(url));
                                context.startActivity(intent);
                            } else if (Utility.isWeiboAccountDomainLink(url)) {
                                Intent intent = new Intent(context, UserInfoActivity.class);
                                intent.putExtra("domain", Utility.getDomainFromWeiboAccountLink(url));
                                context.startActivity(intent);
                            } else {
                                WebBrowserSelector.openLink(context, uri);
                            }
                        } else {
                            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                            intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
                            context.startActivity(intent);
                        }
                        break;
                    case 1:
                        ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                        cm.setPrimaryClip(ClipData.newPlainText("sinaweibo", getStringContent()));
                        Toast.makeText(
                                BeeboApplication.getInstance(),
                                String.format(BeeboApplication.getInstance().getString(R.string.have_copied),
                                        getStringContent()),
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

        return builder.create();
    }

    private String getStringContent() {
        String d = uri.toString();
        String newValue = "";
        if (d.startsWith("org.zarroboogs.weibo")) {
            int index = d.lastIndexOf("/");
            newValue = d.substring(index + 1);
        } else if (d.startsWith("http")) {
            newValue = d;
        }
        return newValue;
    }
}
