package com.tagmae.tsgame_erwang;

import android.app.Activity;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tsgame.tsgame_niuniu.simcpux.bean.sharetypeBean;

/**
 * 分享面板控制辅助类
 */
public class SharePanelHelper {

    // 保存当前分享的数据
    private static sharetypeBean currentShareData;    /**
     * 显示分享面板并保存分享数据
     * @param activity 当前活动
     * @param shareData 分享数据
     */
    public static void showSharePanel(Activity activity, String shareData) {
        try {
            // 保存分享数据
            Gson gson = new Gson();
            currentShareData = gson.fromJson(shareData, sharetypeBean.class);

            // 显示分享面板
            final View sharePanel = activity.findViewById(com.jx.jyhd.R.id.share_panel);
            if (sharePanel != null) {
                // 确保每次显示面板都重新设置点击事件
                setupPanelClickListeners(activity);

                // 确保先初始化面板上的按钮
                initSharePanelButtons(activity);

                // 兼容低版本，使用AlphaAnimation代替setAlpha
                sharePanel.setVisibility(View.VISIBLE);
                if (android.os.Build.VERSION.SDK_INT >= 11) {
                    // API 11+使用View.setAlpha
                    sharePanel.setAlpha(0f);
                    sharePanel.animate().alpha(1f).setDuration(300).start();
                } else {
                    // 低版本使用Animation
                    android.view.animation.AlphaAnimation animation = new android.view.animation.AlphaAnimation(0f, 1f);
                    animation.setDuration(300);
                    animation.setFillAfter(true);
                    sharePanel.startAnimation(animation);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            //Toast.makeText(activity, "打开分享面板失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 设置面板点击事件处理
     */
    private static void setupPanelClickListeners(final Activity activity) {
        try {
            // 获取面板和内容容器
            final View sharePanel = activity.findViewById(com.jx.jyhd.R.id.share_panel);
            final View container = activity.findViewById(com.jx.jyhd.R.id.share_panel_container);

            if (sharePanel == null || container == null) return;

            // 重要：先移除所有已存在的点击监听器，避免重复添加
            if (android.os.Build.VERSION.SDK_INT >= 16) {
                sharePanel.setOnClickListener(null);
                container.setOnClickListener(null);
            }

            // 设置点击面板空白区域关闭面板
            sharePanel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 当点击到面板空白区域时，关闭面板
                    //Toast.makeText(activity, "关闭分享面板", Toast.LENGTH_SHORT).show();
                    hideSharePanel(activity);
                }
            });

            // 防止点击事件传递到下层视图（重要：这里使用onClickListener来消费事件）
            container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 点击内容区域不关闭面板，消费掉事件
                    // 空实现即可，目的是阻止事件传递到sharePanel
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 隐藏分享面板
     * @param activity 当前活动
     */
    public static void hideSharePanel(Activity activity) {
        final View sharePanel = activity.findViewById(com.jx.jyhd.R.id.share_panel);
        if (sharePanel != null) {
            if (android.os.Build.VERSION.SDK_INT >= 11) {
                // API 11+使用View.animate().alpha
                sharePanel.animate().alpha(0f).setDuration(300).withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        sharePanel.setVisibility(View.GONE);
                    }
                }).start();
            } else {
                // 低版本使用Animation
                android.view.animation.AlphaAnimation animation = new android.view.animation.AlphaAnimation(1f, 0f);
                animation.setDuration(300);
                animation.setFillAfter(true);
                animation.setAnimationListener(new android.view.animation.Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(android.view.animation.Animation animation) {}

                    @Override
                    public void onAnimationEnd(android.view.animation.Animation animation) {
                        sharePanel.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(android.view.animation.Animation animation) {}
                });
                sharePanel.startAnimation(animation);
            }
        }
    }

    /**
     * 初始化分享面板
     * @param activity 当前活动
     */
    public static void initSharePanel(final Activity activity) {
        try {
            final View sharePanel = activity.findViewById(com.jx.jyhd.R.id.share_panel);
            if (sharePanel == null) return;

            // 点击面板背景区域关闭面板
            sharePanel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hideSharePanel(activity);
                }
            });

            // 防止点击事件传递到下层视图
            activity.findViewById(com.jx.jyhd.R.id.share_panel_container).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 拦截点击事件，防止关闭面板
                }
            });

            // 微信分享按钮点击事件
            activity.findViewById(com.jx.jyhd.R.id.btn_share_wechat).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentShareData != null) {
                        // 调用微信分享
                        Toast.makeText(activity, "微信分享", Toast.LENGTH_SHORT).show();
                        hideSharePanel(activity);
                    }
                }
            });

            // QQ分享按钮点击事件
            activity.findViewById(com.jx.jyhd.R.id.btn_share_qq).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentShareData != null) {
                        // 调用QQ分享
                        QQIntentShareHelper.doQQShare(activity, new Gson().toJson(currentShareData));
                        Toast.makeText(activity, "QQ分享", Toast.LENGTH_SHORT).show();
                        hideSharePanel(activity);
                    }
                }
            });

            // 抖音分享按钮点击事件
            activity.findViewById(com.jx.jyhd.R.id.btn_share_douyin).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentShareData != null) {
                        // 调用抖音分享
                        DouYinIntentShareHelper.doDouYinShare(activity, new Gson().toJson(currentShareData));
                        Toast.makeText(activity, "抖音分享", Toast.LENGTH_SHORT).show();
                        hideSharePanel(activity);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化分享面板按钮
     * @param activity 当前活动
     */
    private static void initSharePanelButtons(final Activity activity) {
        try {
            final View wechatBtn = activity.findViewById(com.jx.jyhd.R.id.btn_share_wechat);
            final View qqBtn = activity.findViewById(com.jx.jyhd.R.id.btn_share_qq);
            final View douyinBtn = activity.findViewById(com.jx.jyhd.R.id.btn_share_douyin);

            // 确保找到所有按钮
            if (wechatBtn == null || qqBtn == null || douyinBtn == null) {
                Toast.makeText(activity, "初始化分享按钮失败，找不到按钮资源", Toast.LENGTH_SHORT).show();
                return;
            }

            // 微信分享按钮点击事件
            wechatBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(activity, "微信分享按钮已点击", Toast.LENGTH_SHORT).show();
                    if (currentShareData != null) {                        try {
                            WeChatShareHelper.doWeChatShare(activity, new Gson().toJson(currentShareData));
//                            // 实现微信分享功能
//                            if (currentShareData.getType() != null && currentShareData.getType().equals("3")) {
//                                // 图片分享
//                                Toast.makeText(activity, "调用微信图片分享", Toast.LENGTH_SHORT).show();
//                            } else {
//                                // 网页分享
//                                Toast.makeText(activity, "调用微信网页分享", Toast.LENGTH_SHORT).show();
//                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            //Toast.makeText(activity, "微信分享失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                        hideSharePanel(activity);
                    }
                }
            });

            // QQ分享按钮点击事件
            qqBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(activity, "QQ分享按钮已点击", Toast.LENGTH_SHORT).show();
                    if (currentShareData != null) {
                        try {
                            // 调用QQ分享
                            QQIntentShareHelper.doQQShare(activity, new Gson().toJson(currentShareData));
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(activity, "QQ分享失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                        hideSharePanel(activity);
                    }
                }
            });

            // 抖音分享按钮点击事件
            douyinBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(activity, "抖音分享按钮已点击", Toast.LENGTH_SHORT).show();
                    if (currentShareData != null) {
                        try {
                            // 调用抖音分享
                            DouYinIntentShareHelper.doDouYinShare(activity, new Gson().toJson(currentShareData));
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(activity, "抖音分享失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                        hideSharePanel(activity);
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            //Toast.makeText(activity, "初始化分享按钮失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 获取当前分享数据
     */
    public static sharetypeBean getCurrentShareData() {
        return currentShareData;
    }
}
