package com.sheep.gamegroup.view.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.StringDef;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.StringUtils;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by ljy on 2018/6/22.
 */

public class DialogShare {
    public final static String FACE_TO_FACE = "face_to_face";
    public final static String COPY_SHARE_LINK = "copy_share_link";
    public final static String WEIXIN_CIRCLE = "weixin_circle";
    public final static String QQ = "qq";
    public final static String WX = "wx";
    public final static String ALL = "all";
    public final static String OTHER = "other";

    @StringDef({FACE_TO_FACE, COPY_SHARE_LINK, WEIXIN_CIRCLE, QQ, WX, OTHER, ALL})
    @Retention(RetentionPolicy.SOURCE)
    public @interface SHARE_TYPE {
    }

    private Activity activity;
    private String link;
    private String description;

    public DialogShare(Activity activity, String link, String description) {
        this.activity = activity;
        this.link = link;
        this.description = description;
    }

    public void showShare() {
        if (activity == null) {
            return;
        }
        View dialog_parent = View.inflate(activity, R.layout.share_panel_layout, null);
        final AlertDialog dialog = new AlertDialog.Builder(activity, R.style.AppTheme_Dialog_Alert)
                .setView(dialog_parent)
                .create();
        LinearLayout qq_layotu = dialog_parent.findViewById(R.id.qq_layotu);
        LinearLayout weixin_layotu = dialog_parent.findViewById(R.id.weixin_layotu);
        TextView withdrawal = dialog_parent.findViewById(R.id.withdrawal);
        TextView tv_link = dialog_parent.findViewById(R.id.tv_link);
        TextView cancel_share_tv = dialog_parent.findViewById(R.id.cancel_share_tv);
        if (!TextUtils.isEmpty(link))
            tv_link.setText(link);

        withdrawal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringUtils.CopyText(link);
                G.showToast("复制链接成功");
            }
        });


        qq_layotu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtil.getInstance().popShare(activity, link, QQ, description);

                dialog.dismiss();
            }
        });
        weixin_layotu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtil.getInstance().popShare(activity, link, WX, description);
                dialog.dismiss();
            }
        });

        cancel_share_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
            }
        });
        try {
            dialog.show();
            Window window = dialog.getWindow();
            if(window != null) {
                window.setGravity(Gravity.BOTTOM);
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = G.WIDTH > G.HEIGHT ? G.HEIGHT : G.WIDTH;
                window.setAttributes(params);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
