package com.tencent.qcloud.tuikit.tuichat.ui.view.input.inputmore;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.tencent.qcloud.tuicore.component.fragments.BaseFragment;
import com.tencent.qcloud.tuikit.tuichat.R;
import com.tencent.qcloud.tuikit.tuichat.bean.ChatCustomGiftMessage;
import com.tencent.qcloud.tuikit.tuichat.bean.message.TUIMessageBean;
import com.tencent.qcloud.tuikit.tuichat.ui.view.input.InputView;
import com.tencent.qcloud.tuikit.tuichat.util.ChatMessageBuilder;
import com.tencent.qcloud.tuikit.tuichat.util.ChatSendGiftView;
import com.tencent.qcloud.tuikit.tuichat.widget.ConversionChatGiftAdapter;
import com.tencent.qcloud.tuikit.tuichat.widget.CustomRecyclerView;
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.pay.StarCoinPayActivity;
import com.zxhl.cms.utils.Utils;
import com.zxhl.cms.widget.ChatDialogUtils;

/**
 * @Description: java类作用描述
 * @Author: 哈兹米
 * @CreateDate: 2021/12/25 5:57 下午
 */
public class ConversionChatGiftFragment extends BaseFragment {

    CustomRecyclerView mConversionChatGiftRV;
    ConversionChatGiftAdapter mAdapter;

    ChatGiftListBackEntity giftInfo = null;
    private InputView.MessageHandler mMessageHandler;
    private String chatUserId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments == null || arguments.getSerializable("giftInfo") == null) {
            return;
        }
        giftInfo = (ChatGiftListBackEntity) arguments.getSerializable("giftInfo");
        chatUserId = arguments.getString("chatUserId");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View baseView = inflater.inflate(R.layout.fragment_conversion_chat_gift_child, container, false);

        return baseView;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mConversionChatGiftRV = view.findViewById(R.id.conversion_chat_gift_rv);
        mAdapter = new ConversionChatGiftAdapter(null);
        mConversionChatGiftRV.setLayoutManager(new GridLayoutManager(getContext(), 2, GridLayoutManager.HORIZONTAL, false));
        mConversionChatGiftRV.setAdapter(mAdapter);
        mConversionChatGiftRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }
        });

        mAdapter.setOnItemClickListener((adapter1, view1, position) -> {
            if (adapter1.getItem(position) == null || TextUtils.isEmpty(chatUserId)) {
                return;
            }
            ChatGiftListBackEntity.ChatGiftListEntity item = (ChatGiftListBackEntity.ChatGiftListEntity) adapter1.getItem(position);
            UserInfo userInfo = SettingPreference.getUserInfoData();
            try {
                if (userInfo.starBalance < Integer.parseInt(item.priceStr)) {
                    ChatDialogUtils.showMemberTrip(getActivity(), new ChatDialogUtils.ChatDialogCallBack() {
                        @Override
                        public void confirm() {
                            Intent intent = new Intent(getContext(), StarCoinPayActivity.class);
                            startActivity(intent);
                        }

                        @Override
                        public void cancel() {

                        }
                    });
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (view1 instanceof LinearLayout) {
                AppCompatImageView giftIconIV = (AppCompatImageView) ((LinearLayout) view1).getChildAt(0);
                //发送完礼物后，更新星币数据
                ApiClient.INSTANCE.getChatApi().chatSendGift(item.id, chatUserId).compose(RxSchedulers.observableIO2Main()).subscribe(new BaseObserver<Object>() {
                    @Override
                    public void onSuccess(Object result) {
                        Gson gson = new Gson();
                        ChatCustomGiftMessage customGiftMessage = new ChatCustomGiftMessage();
                        customGiftMessage.giftUrl = item.img;
                        String data = gson.toJson(customGiftMessage);
                        TUIMessageBean info = ChatMessageBuilder.buildCustomMessage(data, getString(R.string.chat_gift_message), customGiftMessage.giftUrl.getBytes());
                        info.setExtra(getString(R.string.chat_gift_message));
                        InputView.sendCustomMessage(info);
                        //发送完礼物后，更新星币数据
                        ChatSendGiftView.sendGiftAnimation(getActivity(), view1, giftIconIV.getDrawable());
                    }

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

        if (giftInfo != null) {
            mAdapter.setNewInstance(giftInfo.giftList);
        }

    }

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

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