package com.tencent.qcloud.tuikit.tuigroup.ui.view;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.Nullable;

import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.tencent.qcloud.tuicore.TUICore;
import com.tencent.qcloud.tuicore.component.TitleBarLayout;
import com.tencent.qcloud.tuicore.component.interfaces.ITitleBarLayout;
import com.tencent.qcloud.tuikit.tuigroup.R;
import com.tencent.qcloud.tuikit.tuigroup.TUIGroupConstants;
import com.tencent.qcloud.tuikit.tuigroup.bean.GroupApplyInfo;
import com.tencent.qcloud.tuikit.tuigroup.bean.GroupInfo;
import com.tencent.qcloud.tuikit.tuigroup.ui.interfaces.IGroupApplyLayout;
import com.tencent.qcloud.tuikit.tuigroup.presenter.GroupApplyPresenter;

import java.util.List;


public class GroupApplyManagerLayout extends LinearLayout implements IGroupApplyLayout {

    private TitleBarLayout mTitleBar;
    private ListView mApplyMemberList;
    private GroupApplyAdapter mAdapter;

    private GroupApplyPresenter presenter;
    public GroupApplyManagerLayout(Context context) {
        super(context);
        init();
    }

    public GroupApplyManagerLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GroupApplyManagerLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.group_apply_manager_layout, this);
        mApplyMemberList = findViewById(R.id.group_apply_members);
        mAdapter = new GroupApplyAdapter();
        mAdapter.setOnItemClickListener(new GroupApplyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(GroupApplyInfo info) {
                Bundle bundle = new Bundle();
                bundle.putString("fromUser", info.getFromUser());
                bundle.putString("fromUserNickName", info.getFromUserNickName());
                bundle.putString("requestMsg", info.getRequestMsg());
                bundle.putSerializable("groupApplication", info.getGroupApplication());
                TUICore.startActivity(getContext(),"FriendProfileActivity", bundle, TUIGroupConstants.ActivityRequest.CODE_1);
            }
        });
        mApplyMemberList.setAdapter(mAdapter);
        mTitleBar = findViewById(R.id.group_apply_title_bar);
        mTitleBar.getRightGroup().setVisibility(View.GONE);
        mTitleBar.setTitle(getResources().getString(R.string.group_apply_members), ITitleBarLayout.Position.MIDDLE);
        mTitleBar.setOnLeftClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO 通知群聊模块群聊刷新 UI
//                GroupChatEventListener groupChatManagerKit = TUIKitPresenterManager.getInstance().getGroupChatEventListener();
//                if (groupChatManagerKit != null) {
//                    groupChatManagerKit.onApplied(mAdapter.getUnHandledSize());
//                }
                if (getContext() instanceof Activity) {
                    ((Activity) getContext()).finish();
                }
            }
        });
    }

    public TitleBarLayout getTitleBar() {
        return mTitleBar;
    }

    public void updateItemData(GroupApplyInfo info) {
        mAdapter.updateItemData(info);
    }

    public void onDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }

    public void setPresenter(GroupApplyPresenter groupApplyPresenter) {
        this.presenter = groupApplyPresenter;
        mAdapter.setPresenter(groupApplyPresenter);
    }

    public void onGroupApplyInfoListChanged(List<GroupApplyInfo> applyInfoList) {
        mAdapter.setDataSource(applyInfoList);
    }

    public void setDataSource(GroupInfo dataSource) {
        presenter.setGroupInfo(dataSource);
        mAdapter.setDataSource(dataSource);
    }
}
