package com.yyrh.ui.floatview;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import cn.yyxx.support.DensityUtils;
import cn.yyxx.support.ResUtils;

/**
 * @author #Suyghur.
 * Created on 2021/10/21
 */
public class FloatingBallMenu {

    private IFloatingBallMenuCallback callback;
    public View contentView;
    private LinearLayout containerLayout;
    private GridView menuView;
    private ArrayList<Item> items = new ArrayList<>();

    public FloatingBallMenu(Context context, IFloatingBallMenuCallback callback) {
        this.callback = callback;
        contentView = LayoutInflater.from(context).inflate(ResUtils.getResId(context, "hnyy_floating_ball_menu", "layout"), null);

        containerLayout = contentView.findViewById(ResUtils.getResId(context, "hnyy_ll_container", "id"));
        containerLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);

        menuView = contentView.findViewById(ResUtils.getResId(context, "hnyy_gv_menu", "id"));

        // 重新设置menuView宽高
        int menuWidth = DensityUtils.dip2px(context, callback.onInitMenuData().size() * 45f);
        LinearLayout.LayoutParams menuParams = new LinearLayout.LayoutParams(menuWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
        menuParams.setMargins(0, 0, DensityUtils.dip2px(context, 5f), 0);
        menuView.setLayoutParams(menuParams);

        items = callback.onInitMenuData();
        menuView.setNumColumns(items.size());
        menuView.setAdapter(new FloatingBallMenuAdapter(context, items));
    }

    public void updateLayout(boolean isLeftLocation) {
        menuView.bringToFront();
        if (isLeftLocation) {
            containerLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
        } else {
            containerLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        }
    }


    public interface IFloatingBallMenuCallback {

        ArrayList<Item> onInitMenuData();

        void onDismissMenu();

        void onMenuItemCLick(Item item, int pos);
    }

    public static class Item {
        public FloatFeature feature;
        public String url;

        public Item(FloatFeature feature, String url) {
            this.feature = feature;
            this.url = url;
        }
    }

    public class FloatingBallMenuAdapter extends BaseAdapter {
        private Context context;
        private ArrayList<Item> items;

        public FloatingBallMenuAdapter(Context context, ArrayList<Item> items) {
            this.context = context;
            this.items = items;
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final Item item = items.get(position);
            View view = View.inflate(context, ResUtils.getResId(context, "hnyy_floating_ball_menu_item", "layout"), null);
            ImageView ivIcon = view.findViewById(ResUtils.getResId(context, "hnyy_iv_icon", "id"));
            switch (item.feature) {
                case ACCOUNT_CENTER:
                    ivIcon.setBackgroundResource(ResUtils.getResId(context, "hnyy_account_center_img", "drawable"));
                    break;
                case SWITCH_ACCOUNT:
                    ivIcon.setBackgroundResource(ResUtils.getResId(context, "hnyy_switch_account_img", "drawable"));
                    break;
                case MENU_DISMISS:
                    ivIcon.setBackgroundResource(ResUtils.getResId(context, "hnyy_menu_dismiss_img", "drawable"));
                    break;
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callback.onMenuItemCLick(item, position);
                }
            });

            return view;
        }
    }

    public enum FloatFeature {
        ACCOUNT_CENTER,
        SWITCH_ACCOUNT,
        MENU_DISMISS
    }
}
