package com.yyrh.ui.floatmenu;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;


import com.yyrh.constant.SDKSettings;
import com.yyrh.sdk.YyrhSdkManager;

import com.yyrh.ui.dialog.UserCenterDialog;

import com.yyrh.utils.ResourceUtil;
import com.yyrh.utils.YYLog;


import static com.yyrh.constant.Constants.CLICK_INTERVAL;
import static com.yyrh.constant.SDKSettings.isOpenFloatView;

public class FloatViewManager {

    private static FloatViewManager manager = null;

    FloatLogoMenu mFloatMenu;

    private FloatLogoMenu.Builder builder;

    public final static int SHOW_FLOAT  = 0;

    public final static int DESTORY_FLOAT  = 1;

    private long clickLoginTime = 0L;


    private FloatViewManager(Context context) {
        super();

        if (isOpenFloatView == true){

            initView(context);
        }

    }

    public static FloatViewManager defaultManager(Context context) {

        if (manager == null) {
            synchronized (YyrhSdkManager.class) {
                if (manager == null) {
                    manager = new FloatViewManager(context);
                }
            }
        }

        return manager;
    }

    public void initView(final Context context){

        if (builder == null) {

            builder = new FloatLogoMenu.Builder()
                    .withActivity((Activity) context)
                    .logo(BitmapFactory.decodeResource(context.getResources(),ResourceUtil.getMipmapId(context,"hnyy_float_icon")))
                    .setBgDrawable(context.getResources().getDrawable(ResourceUtil.getDrawableId(context,"hnyy_game_float_menu_bg")))
                    .defaultLocation(FloatLogoMenu.LEFT)
                    .setOnFloatClickListener(new FloatLogoMenu.OnFloatClickListener() {
                        @Override
                        public void onClick() {

                            if (System.currentTimeMillis() - clickLoginTime < CLICK_INTERVAL) {
                                YYLog.i("调用悬浮窗按钮太频繁，拦截此次操作");
                                return;
                            }
                            clickLoginTime = System.currentTimeMillis();
                            new UserCenterDialog(context,ResourceUtil.getStyleId(context,"hnyy_half_black_dialog"), SDKSettings.loginData.user_center).show();
                        }
                    });




        }
    }

    public void showFloat() {
        if (builder != null) {
            mFloatMenu = builder.show();

        }
    }


    public void destroyFloat() {
        if (mFloatMenu != null) {
            mFloatMenu.destoryFloat();
        }

        mFloatMenu = null;
        manager = null;
        builder = null;
    }


}
