package com.tencent.liteav.trtcvoiceroom.ui.widget;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tencent.liteav.trtcvoiceroom.R;
import com.zxhl.cms.common.base.BaseFragment;
import com.zxhl.cms.net.ApiClient;
import com.zxhl.cms.net.RxSchedulers;
import com.zxhl.cms.net.SettingPreference;
import com.zxhl.cms.net.callback.BaseObserver;
import com.zxhl.cms.net.model.chat.ChatGiftListBackEntity;
import com.zxhl.cms.net.model.uc.UserInfo;
import com.zxhl.cms.utils.OnRecycleItemClickListener;
import com.zxhl.cms.utils.Utils;
import com.zxhl.cms.widget.ChatSendGiftView;

public class GiftChildFragment extends BaseFragment implements OnRecycleItemClickListener<ChatGiftListBackEntity.ChatGiftListEntity> {
    ChatGiftListBackEntity giftInfo = null;
    private String chatUserId;
    private RecyclerView rlgift;
    private GiftChildAdapter mAdapter;

    @Override
    public int layoutID() {
        return R.layout.fragment_layout_child;
    }

    @Override
    public void init(View view) {
        Bundle arguments = getArguments();
        if (arguments == null || arguments.getSerializable("giftInfo") == null) {
            return;
        }
        giftInfo = (ChatGiftListBackEntity) arguments.getSerializable("giftInfo");
        chatUserId = arguments.getString("chatUserId");
        rlgift = view.findViewById(R.id.id_rl_gift_list);
        mAdapter = new GiftChildAdapter(this);
        rlgift.setLayoutManager(new GridLayoutManager(getContext(), 2, GridLayoutManager.HORIZONTAL, false));
        rlgift.setAdapter(mAdapter);
        mAdapter.appendToList(giftInfo.giftList);
        mAdapter.notifyDataSetChanged();
    }


    @Override
    public void onItemClick(View view, int position, ChatGiftListBackEntity.ChatGiftListEntity data) {
        if(TextUtils.isEmpty(chatUserId)){
            return;
        }
        UserInfo userInfo = SettingPreference.getUserInfoData();
        try {
            if (userInfo.starBalance < Integer.parseInt(data.priceStr)) {
                Utils.showToast(mActivity,"余额不足");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (view instanceof LinearLayout) {
            AppCompatImageView giftIconIV = (AppCompatImageView) ((LinearLayout) view).getChildAt(0);
            //发送完礼物后，更新星币数据
            ApiClient.INSTANCE.getChatApi().chatSendGift(data.id, chatUserId).compose(RxSchedulers.observableIO2Main()).subscribe(new BaseObserver<Object>() {
                @Override
                public void onSuccess(Object result) {
                    //发送完礼物后，更新星币数据
                    ChatSendGiftView.sendGiftAnimation(getActivity(), view, giftIconIV.getDrawable());
                }

                @Override
                public void onFailure(Throwable e, String code, String errorMsg) {
                    Utils.showToast(getContext(), errorMsg);
                }
            });

        }
    }
}
