package com.bc_chat.im.activity.envelopes;

import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bc_chat.TipsMessage;
import com.bc_chat.bc_base.config.RouteConfig;
import com.bc_chat.bc_base.contract.FriendApplyRecordContract;
import com.bc_chat.bc_base.entity.FriendApplyRecordBean;
import com.bc_chat.bc_base.presenter.FriendApplyRecordPresenter;
import com.bc_chat.bc_base.utils.Const;
import com.bc_chat.im.R;
import com.bc_chat.im.activity.envelopes.adapter.FriendApplyRecordAdapter;
import com.zhaohaoting.framework.abs.BaseActivity;
import com.zhaohaoting.framework.imagepicker.camera.util.LogUtil;
import com.zhaohaoting.framework.utils.ToastUtils;
import com.zhaohaoting.framework.utils.observer.ObserverManager;
import com.zhaohaoting.framework.view.recyclerview.CustomRecyclerView;
import com.zhaohaoting.framework.view.recyclerview.WrapContentLinearLayoutManager;

import org.jetbrains.annotations.Nullable;

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

import io.rong.imkit.RongIM;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

/**
 * 我的钱包
 */
@Route(path = RouteConfig.FRIEND_APPLY_RECORD_ACTIVITY)
public class FriendApplyRecordActivity extends BaseActivity<FriendApplyRecordPresenter> implements FriendApplyRecordContract.View, SwipeRefreshLayout.OnRefreshListener {

    //列表
    public CustomRecyclerView listView;
    //通用的adapter
    public FriendApplyRecordAdapter adapter;
    //下拉刷新
    public SwipeRefreshLayout swipeRefreshLayout;

    List<FriendApplyRecordBean> FriendApplyRecordBeanList = new ArrayList<>();

    int currentType = 0;
    String currentAddUserId;
    private FriendApplyRecordBean dataBean;

    @Override
    protected FriendApplyRecordPresenter initPresenter() {
        return new FriendApplyRecordPresenter<>(this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_withdrawal_record;
    }

    @Override
    protected void initialize() {
        initView();
        initData();


    }

    private void initData() {
        getPresenter().applyFriendList(1, 1000);
    }

    private void initListView() {
        listView = (CustomRecyclerView) findViewById(R.id.rv_list);
        listView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayout.VERTICAL, false));
        listView.setNestedScrollingEnabled(false);

        //init swiperefreshlayout
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_refresh);
        swipeRefreshLayout.setColorSchemeColors(
                getResources().getColor(R.color.colorPrimaryDark), getResources().getColor(R.color.colorPrimary), getResources().getColor(R.color.colorAccent));
        swipeRefreshLayout.setOnRefreshListener(this);

        adapter = new FriendApplyRecordAdapter(this, FriendApplyRecordBeanList);
        adapter.setFriendHandlerListener(new FriendApplyRecordAdapter.IFriendHandlerListener() {
            @Override
            public void onRefuse(int position) {
                dataBean = FriendApplyRecordBeanList.get(position);
                if (dataBean != null) {
                    currentType = 3;

                    getPresenter().handleFriendApply(dataBean.getId(), currentType);


                }
            }

            @Override
            public void onAgree(int position) {
                dataBean = FriendApplyRecordBeanList.get(position);
                if (dataBean != null) {
                    currentType = 2;
                    currentAddUserId = dataBean.getCuid();
                    getPresenter().handleFriendApply(dataBean.getId(), currentType);

                }
            }
        });
        listView.setAdapter(adapter);
    }

    private void setTipsMsg() {
        TipsMessage tipsMessage = TipsMessage.obtain(dataBean.getUid(), "1", "");
        RongIM.getInstance().sendMessage(Message.obtain(dataBean.getUid(), Conversation.ConversationType.PUSH_SERVICE, tipsMessage),
                "好友请求", null, new IRongCallback.ISendMessageCallback() {
                    @Override
                    public void onAttached(Message message) {
                        LogUtil.d(message.toString());
                    }

                    @Override
                    public void onSuccess(Message message) {
                        LogUtil.d(message.toString());
                    }

                    @Override
                    public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                        LogUtil.d(message.toString());
                    }
                });
    }

    private void initView() {
        initListView();
    }


    @Override
    public void onRefresh() {
        getPresenter().applyFriendList(1, 1000);
    }

    /**
     * 隐藏下拉刷新按钮
     */
    public void hideSwipeRefreshLayout() {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void applyFriendListSuccess(@Nullable List<? extends FriendApplyRecordBean> dataList) {
        if (dataList != null) {
            FriendApplyRecordBeanList = (List<FriendApplyRecordBean>) dataList;
            adapter.setDataCoverList(FriendApplyRecordBeanList);
            adapter.notifyDataSetChanged();
            hideSwipeRefreshLayout();
        }
    }

    @Override
    public void applyFriendListFailure(@Nullable Exception exception) {
        hideSwipeRefreshLayout();
    }

    @Override
    public void handleFriendApplySuccess() {
        if (currentType == 2) {
            ToastUtils.show("已同意");
            ObserverManager.getInstance().notifyObserver(Const.ObserverKey.UPDATE_FRIEND_LIST, currentAddUserId);
        } else if (currentType == 3) {
            ToastUtils.show("已拒绝");
        }
        setTipsMsg();
        currentType = 0;
        onRefresh();

    }

    @Override
    public void handleFriendApplyFailure(@Nullable Exception exception) {

    }
}
