package com.sheep.gamegroup.view.dialog;

import android.app.Activity;
import android.content.DialogInterface;
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.StringUtils;
import com.sheep.gamegroup.util.share.ShareLinkConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.umeng.socialize.UMShareListener;

import static com.sheep.gamegroup.util.share.ShareLinkConfig.QQ;
import static com.sheep.gamegroup.util.share.ShareLinkConfig.WX;
/**
 * Created by realicing on 2018/11/23.
 * realicing@sina.com
 * 通过对话框进行分享
 */
public class DialogShare {

    private Activity activity;
    private UMShareListener umShareListener;
    private ShareLinkConfig shareLinkConfig;

    public DialogShare(Activity activity, ShareLinkConfig shareLinkConfig, UMShareListener umShareListener) {
        this.activity = activity;
        this.shareLinkConfig = shareLinkConfig;
        this.umShareListener = umShareListener;
    }

    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(shareLinkConfig.getLink()))
            tv_link.setText(shareLinkConfig.getLink());

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


        qq_layotu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareLinkConfig.setShareType(QQ).toShare(activity, umShareListener);

                dialog.dismiss();
            }
        });
        weixin_layotu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareLinkConfig.setShareType(WX).toShare(activity, umShareListener);
                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();
        }
    }
}
