package com.sxyz.rfid.ui;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sxyz.rfid.R;
import com.sxyz.rfid.base.BaseActivity;
import com.sxyz.rfid.databinding.ActivityHomeBinding;
import com.sxyz.rfid.ui.out.OutboundActivity;
import com.sxyz.rfid.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 主要功能: 首页
 *
 * @Description:
 * @Project: rfid-app
 * @Package: com.sxyz.rfid.ui.ui.MainActivity
 * @author: chunyuanwu
 * @date: 2025年12月27日 16:40
 * @version: 1.0.0
 */
public class MainActivity extends BaseActivity<ActivityHomeBinding> {
    private List<DataBean> mDataList = new ArrayList<>();
    private HomeAdapter adapter;

    @Override
    public void initParms(Bundle mBundle) {

    }

    @Override
    protected ActivityHomeBinding getViewBinding() {
        return ActivityHomeBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        initToolBarTitle("仓储管理");
        mDataList.add(new DataBean("出库管理", "处理商品出库操作,扫码更新库存信息", R.mipmap.outbound_icon, R.drawable.blue_round_bg, R.drawable.up_blue_line_round));
        mDataList.add(new DataBean("库存盘点", "执行库存盘点，核对库存数量", R.mipmap.review_icon, R.drawable.orange_round_bg, R.drawable.up_orange_line_round));
        // 设置3列的网格布局
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        mBinding.recyclerView.setLayoutManager(layoutManager);
        adapter = new HomeAdapter(R.layout.item_home, mDataList);
        mBinding.recyclerView.setAdapter(adapter);
    }

    @Override
    protected void setListener() {
        adapter.setOnItemClickListener((position, item) -> {
            ToastUtil.showShort(item.getName());
            switch (position) {
                case 0://出库管理
                    startActivity(OutboundActivity.class);
                    break;
                case 1://库存盘点
//                    startActivity(InventoryActivity.class);
                    break;
            }
        });
    }

    @Override
    protected void initData() {

    }
}

class DataBean {
    public DataBean(String name, String tag, int img, int imgBg, int imgLine) {
        this.name = name;
        this.tag = tag;
        this.img = img;
        this.imgBg = imgBg;
        this.imgLine = imgLine;
    }

    String name;
    String tag;
    int img;
    int imgBg;
    int imgLine;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }

    public int getImgBg() {
        return imgBg;
    }

    public void setImgBg(int imgBg) {
        this.imgBg = imgBg;
    }

    public int getImgLine() {
        return imgLine;
    }

    public void setImgLine(int imgLine) {
        this.imgLine = imgLine;
    }
}

class HomeAdapter extends BaseQuickAdapter<DataBean, BaseViewHolder> {

    public HomeAdapter(int layoutResId, List<DataBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, DataBean dataBean) {
        baseViewHolder.setText(R.id.tv_title, dataBean.getName());
        baseViewHolder.setText(R.id.tv_tag, dataBean.getTag());
        baseViewHolder.setImageResource(R.id.iv_background, dataBean.getImg());
        baseViewHolder.setBackgroundResource(R.id.iv_background, dataBean.getImgBg());
        baseViewHolder.setBackgroundResource(R.id.v_line, dataBean.getImgLine());
        baseViewHolder.itemView.setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(baseViewHolder.getAdapterPosition(), dataBean);
                //实现CardView点击放大缩小效果
                //使用ObjectAnimator实现平滑缩放动画
                //先放大10%再恢复原状
                //动画持续时间为300毫秒
                ObjectAnimator scaleUpX = ObjectAnimator.ofFloat(v, "scaleX", 1.0f, 1.1f);
                ObjectAnimator scaleUpY = ObjectAnimator.ofFloat(v, "scaleY", 1.0f, 1.1f);
                scaleUpX.setDuration(150);
                scaleUpY.setDuration(150);
                scaleUpX.start();
                scaleUpY.start();
                scaleUpX.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(v, "scaleX", 1.1f, 1.0f);
                        ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(v, "scaleY", 1.1f, 1.0f);
                        scaleDownX.setDuration(150);
                        scaleDownY.setDuration(150);
                        scaleDownX.start();
                        scaleDownY.start();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }
                });
            }
        });
    }

    public interface OnItemClickListener {
        void onItemClick(int position, DataBean item);
    }

    private OnItemClickListener mItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }
}