package ai.pai.client.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.views.swiperefreshrecyclerview.DividerItemDecoration;

import java.util.ArrayList;

import ai.pai.client.R;
import ai.pai.client.adapter.GroupMemberAdapter;
import ai.pai.client.beans.GroupMemberInfo;
import ai.pai.client.services.GroupService;
import ai.pai.client.utils.SystemUtils;

public class GroupDeleteMemberActivity extends BaseActivity implements View.OnClickListener,GroupService.GroupServiceListener{

    private boolean isBound;
    private GroupService groupService;
    private GroupServiceConnection serviceConnection;
    private GroupMemberAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_member);
        SystemUtils.setImmerseLayout(this,findViewById(R.id.title_layout));
        TextView title = (TextView)findViewById(R.id.title_bar_middle_txt);
        title.setText(R.string.group_member);
        findViewById(R.id.title_bar_back_layout).setOnClickListener(this);
        findViewById(R.id.title_bar_option_layout).setVisibility(View.INVISIBLE);

        String groupId = getIntent().getStringExtra("group_id");
        boolean isAdmin = getIntent().getBooleanExtra("isAdmin",false);
        ArrayList<GroupMemberInfo> memberList = (ArrayList<GroupMemberInfo>)getIntent().getSerializableExtra("member_list");
        RecyclerView memberRecyclerView = (RecyclerView)findViewById(R.id.recycler_view_members);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        memberRecyclerView.setLayoutManager(layoutManager);
        memberRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
        adapter = new GroupMemberAdapter(this,isAdmin,groupId,memberList);
        memberRecyclerView.setAdapter(adapter);
        serviceConnection = new GroupServiceConnection(this);
        if(!isBound){
            Intent intent = new Intent(this, GroupService.class);
            bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
            isBound = true;
        }
    }

    @Override
    protected void onDestroy() {
        if(groupService!=null){
            groupService.unregisterServiceListener();
        }
        if(isBound){
            unbindService(serviceConnection);
            isBound = false;
        }
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_bar_back_layout:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onCommandCommitFail(int command, String msg) {
        if( GroupService.GroupCommand.COMMAND_DELETE_GROUP_MEMBER == command){
            Toast.makeText(getApplicationContext(), R.string.delete_member_fail,Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCommandCommitSuccess(int command, Object response) {
        if( GroupService.GroupCommand.COMMAND_DELETE_GROUP_MEMBER == command){
            Toast.makeText(getApplicationContext(), R.string.delete_member_success,Toast.LENGTH_SHORT).show();
        }
        if(adapter!=null){
            adapter.notifyDataSetChanged();
        }
    }

    private  class GroupServiceConnection implements ServiceConnection {

        private GroupService.GroupServiceListener listener;

        public GroupServiceConnection(GroupService.GroupServiceListener listener){
            this.listener = listener;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            GroupService.MyBinder binder = (GroupService.MyBinder)service;
            groupService = binder.getService();
            groupService.registerServiceListener(listener);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

}
