package com.yyrh.core.impl.floatball;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.yyrh.core.entity.SdkBackLoginInfo;
import com.yyrh.core.impl.HybridActivity;
import com.yyrh.factory.SdkManager;
import com.yyrh.ui.floatview.FloatingBall;
import com.yyrh.ui.floatview.FloatingBallMenu;

import java.util.ArrayList;

import cn.yyxx.support.ResUtils;

/**
 * @author #Suyghur.
 * Created on 2021/10/21
 */
public class FloatCenterService extends Service {

    private FloatingBall floatingBall = null;
    private Activity activity = null;

    private final FloatingBall.IFloatingBallCallback callback = new FloatingBall.IFloatingBallCallback() {
        @Override
        public void onUpdateBallView(ImageView ballView, boolean isLeftLocation, boolean isHide) {
            ballView.setBackgroundResource(ResUtils.getResId(activity, "hnyy_floating_ball_img", "drawable"));
        }

        @Override
        public ArrayList<FloatingBallMenu.Item> onInitMenuData() {
            ArrayList<FloatingBallMenu.Item> items = new ArrayList<>();
            if (!TextUtils.isEmpty(SdkBackLoginInfo.getInstance().userCenterUrl)) {
                items.add(new FloatingBallMenu.Item(FloatingBallMenu.FloatFeature.ACCOUNT_CENTER, SdkBackLoginInfo.getInstance().userCenterUrl));
            }
            items.add(new FloatingBallMenu.Item(FloatingBallMenu.FloatFeature.SWITCH_ACCOUNT, ""));
            items.add(new FloatingBallMenu.Item(FloatingBallMenu.FloatFeature.MENU_DISMISS, ""));
            return items;
        }

        @Override
        public void onMenuItemClick(FloatingBallMenu.Item item, int pos) {
            switch (item.feature) {
                case ACCOUNT_CENTER:
                    HybridActivity.start(activity, SdkBackLoginInfo.getInstance().userCenterUrl);
                    break;
                case SWITCH_ACCOUNT:
                    SdkManager.defaultManager().logOut();
                    break;
                case MENU_DISMISS:
                    if (floatingBall != null) {
                        floatingBall.dismissMenu();
                    }
                    SdkBackLoginInfo.getInstance().hideFloatCenter = true;
                    detach();
                    break;

            }
        }

        @Override
        public void onExpandMenu(boolean hasRedDot) {

        }

        @Override
        public void onDismissMenu() {
            if (floatingBall != null) {
                floatingBall.dismissMenu();
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public void initService(Activity activity) {
        this.activity = activity;
    }

    public void attach() {
        if (activity == null) {
            return;
        }
        if (SdkBackLoginInfo.getInstance().hideFloatCenter){
            return;
        }
        if (floatingBall == null) {
            floatingBall = new FloatingBall(activity, SdkManager.defaultManager().isLandscape, callback);
        }
        floatingBall.attach();
    }

    public void detach() {
        if (floatingBall != null) {
            floatingBall.detach();
        }
    }

    public void release() {
        if (floatingBall != null) {
            floatingBall.release();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new FloatCenterServiceBinder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    class FloatCenterServiceBinder extends Binder {
        FloatCenterService getService() {
            return FloatCenterService.this;
        }
    }
}
