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.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import ai.pai.client.R;
import ai.pai.client.beans.GroupInfo;
import ai.pai.client.db.DBService;
import ai.pai.client.db.Preferences;
import ai.pai.client.services.GroupService;
import ai.pai.client.utils.SystemUtils;

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

    private boolean isBound;
    private GroupServiceConnection serviceConnection;
    private GroupService groupService;
    private String groupId;
    private String groupName;
    private EditText groupNameEdit;
    private String newGroupName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_rename);
        SystemUtils.setImmerseLayout(this,findViewById(R.id.title_layout));
        TextView title = (TextView)findViewById(R.id.title_bar_middle_txt);
        title.setText(R.string.group_name);
        findViewById(R.id.title_bar_back_layout).setOnClickListener(this);
        findViewById(R.id.btn_save_group_name).setOnClickListener(this);
        findViewById(R.id.title_bar_option_layout).setVisibility(View.INVISIBLE);
        groupNameEdit = (EditText)findViewById(R.id.et_modify_group_name);
        groupId = getIntent().getStringExtra("group_id");
        groupName = getIntent().getStringExtra("group_name");
        groupNameEdit.setText(groupName);
    }

    @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;
            case R.id.btn_save_group_name:
                changeGroupName();
                break;
            default:
                break;
        }
    }

    private void changeGroupName(){

        if(groupNameEdit.getText()!=null){
             newGroupName = groupNameEdit.getText().toString();
        }
        if(newGroupName.replace(" ","").length()==0){
            Toast.makeText(this,R.string.group_name_null,Toast.LENGTH_SHORT).show();
            return;
        }
        if(newGroupName.length()>20){
            Toast.makeText(this,R.string.group_name_too_long,Toast.LENGTH_SHORT).show();
            return;
        }
        if(newGroupName.equals(groupName)){
            Toast.makeText(this,R.string.group_name_same,Toast.LENGTH_SHORT).show();
            return;
        }

        if(!isBound){
            serviceConnection = new GroupServiceConnection(this);
            Intent intent = new Intent(this, GroupService.class);
            bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
            isBound = true;
        }
        Intent intent = new Intent(this, GroupService.class);
        Bundle bundle = new Bundle();
        bundle.putInt("command", GroupService.GroupCommand.COMMAND_UPDATE_GROUP_INFO);
        bundle.putString("group_id",groupId);
        bundle.putString("group_name",newGroupName);
        bundle.putString("admin_id", Preferences.getInstance(this).getUserId());
        bundle.putString("group_desc","");
        intent.putExtras(bundle);
        startService(intent);
    }


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

    @Override
    public void onCommandCommitSuccess(int command, Object response) {
        GroupInfo groupInfo = DBService.getInstance(getApplicationContext()).getGroupInfoByGroupId(groupId);
        groupInfo.groupName = newGroupName;
        DBService.getInstance(getApplicationContext()).updateGroupInfo(groupInfo);
        Toast.makeText(getApplicationContext(), R.string.group_name_modify_success,Toast.LENGTH_SHORT).show();
        Bundle bundle = new Bundle();
        bundle.putString("group_name",newGroupName);
        setResult(RESULT_OK,getIntent().putExtras(bundle));
        finish();
    }

    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) {

        }
    }
}
