package com.tofly.ghrq_route.common.ui.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.lcp.arecyclerview.widget.DividerItemDecoration;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.common.ui.adapter.MessageUserAdapter;
import com.tofly.ghrq_route.module.user.controller.UserCallBack;
import com.tofly.ghrq_route.module.user.controller.UserController;
import com.tofly.ghrq_route.module.user.domain.DepartUserBean;
import com.tofly.ghrq_route.module.user.domain.MessageUserBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;

import org.greenrobot.eventbus.EventBus;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/5/15.
 */

public class UiMessageUserSelect extends BaseUi {
    @BindView(R.id.text_toolbar_title)
    TextView textToolbarTitle;
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.text_empty_view)
    TextView textEmptyView;
    @BindView(R.id.floatbtn_select_all)
    FloatingActionButton floatBtnSelectAll;

    private static final String TAG = UiMessageUserSelect.class.getName();

    private UserBean mUser;
    private UserController userController;

    private List<DepartUserBean> messageUserList;
    private MessageUserAdapter messageUserAdapter;
    private boolean mbIsRefreshing = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ui_message_user_select);
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();
    }

    private void initView() {

    }

    private void initData() {
        mUser = BaseApp.getInstance().getLoginUser();
        messageUserList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        messageUserAdapter = new MessageUserAdapter(this, messageUserList);
        recyclerView.setAdapter(messageUserAdapter);
        swipeRefreshLayout.setRefreshing(true);
        loadUserList();
    }

    private void initListener() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (mbIsRefreshing) {

                } else {
                    loadUserList();
                }
            }
        });

        if (messageUserAdapter != null) {
            messageUserAdapter.setItemClickListener(new MessageUserAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    // 更新 ActionMode 上的计数
                    setSelectedTitle();
                }

                @Override
                public boolean onItemLongClick(View view, int position) {
                    return false;
                }
            });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_confirm, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_all: {
                if (messageUserAdapter != null) {
                    messageUserAdapter.allSelected();
                    setSelectedTitle();
                }
            }
            break;
            case R.id.action_confirm: {
                List<MessageUserBean> userBeanList = messageUserAdapter.getSelectedItems();
                if (userBeanList != null && userBeanList.size() > 0) {
                    EventBus.getDefault().post(new MessageUserSelectEvent(MESSAGE_USER_SELECTED, userBeanList));
                    finish();
                } else {
                    toast("未选择联系人");
                }
            }
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick({R.id.floatbtn_select_all})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.floatbtn_select_all: {
                if (messageUserAdapter != null) {
                    messageUserAdapter.allSelected();
                    setSelectedTitle();
                }
            }
            break;
        }
    }

    private void loadUserList() {
        mbIsRefreshing = true;
        if (userController == null)
            userController = new UserController(this);
        userController.getUserList(mUser.getToken(), new UserCallBack() {
            @Override
            public void onGetUserList(boolean isSuccess, int errorCode, String errMsg, List<DepartUserBean> userList) {
                super.onGetUserList(isSuccess, errorCode, errMsg, userList);
                swipeRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);
                        mbIsRefreshing = false;
                    }
                }, 500);
                recyclerView.setVisibility(View.GONE);
                textEmptyView.setVisibility(View.VISIBLE);
                if (isSuccess && userList != null) {
                    messageUserList = userList;
                    for (DepartUserBean data : messageUserList) {
                        Iterator<MessageUserBean> it=data.getUser().iterator();
                        while (it.hasNext()){
                            MessageUserBean messageUserBean=it.next();
                            if (messageUserBean.getId()==Integer.parseInt(mUser.getUserid())){
                                it.remove();
                            }
                        }
                    }
                    messageUserAdapter.setData(messageUserList);
                    if (userList.size() > 0) {
                        recyclerView.setVisibility(View.VISIBLE);
                        textEmptyView.setVisibility(View.GONE);
                    }
                } else {
                    toast(errMsg);
                }
            }
        });
    }

    private void setSelectedTitle() {
        int size = messageUserAdapter.getSelectionSize();
        if (size > 0) {
            textToolbarTitle.setText("已选中" + String.valueOf(size) + "个");
        } else {
            textToolbarTitle.setText("未选择");
        }
    }

    public final static String MESSAGE_USER_SELECTED = "messageUserSelected";

    public static class MessageUserSelectEvent {
        public final String message;
        public List<MessageUserBean> userList;

        public MessageUserSelectEvent(String message, List<MessageUserBean> users) {
            this.message = message;
            this.userList = users;
        }
    }

    @Override
    protected void onDestroy() {
        BaseApp.cancelAllRequest(getClass().getName());
        super.onDestroy();
    }
}
