package com.ewei.demo.ticket;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.ewei.demo.R;
import com.ewei.demo.widget.tag.TagListView;
import com.ewei.sdk.base.BaseActivity;
import com.ewei.sdk.constants.CommonValue;
import com.ewei.sdk.entity.Tag;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jay on 2018/12/3.
 */
public class OpenTicketTagsActivity extends BaseActivity implements View.OnClickListener,
        TagListView.OnTagClickListener, TextView.OnEditorActionListener, TagListView.OnTagCloseListener {

    private EditText mEtTag;
    private TagListView mTlvTagListCheck;
    private List<Tag> isCheckTagsList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ewei_open_activity_ticket_tag);

        isCheckTagsList = (List<Tag>) getIntent().getSerializableExtra("tags");

        if (isCheckTagsList == null) {
            isCheckTagsList = new ArrayList<>();
        }

        initControl();
    }

    private void initControl() {

        initTitle("编辑标签", CommonValue.TITLE_TYPE_FINISH);
        setFinishClick(this);


        mEtTag = (EditText) findViewById(R.id.et_tag_add);
        mEtTag.setOnEditorActionListener(this);

        mTlvTagListCheck = (TagListView) findViewById(R.id.tlv_tag_list_checked);
        mTlvTagListCheck.setmOnTagCloseListener(this);
        mTlvTagListCheck.setDeleteMode(true);

        if (isCheckTagsList.size() > 0) {
            mTlvTagListCheck.addTags(isCheckTagsList);
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEND || actionId == EditorInfo.IME_ACTION_DONE) {
            String text = v.getText().toString().trim();
            addTag(text);
        }
        return false;
    }

    /**
     * 创建标签
     */
    private void addTag(String name) {
        mTlvTagListCheck.addTag(new Tag(name));
        mEtTag.setText("");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_common_finish:
                hideSoftKeyboard(mEtTag);
                Intent intent = getIntent().putExtra("tags", (Serializable) getSelectedTag());
                setResult(RESULT_OK, intent);
                finish();
                break;
            default:
                break;
        }
    }

    private List<Tag> getSelectedTag() {
        return mTlvTagListCheck.getTags();
    }

    @Override
    public void onTagClick(View tagView, Tag tag) {
        mEtTag.setText("");
        mTlvTagListCheck.addTag(tag);
    }

    @Override
    public void onTagClickClose(View tagView, Tag tag) {
        mTlvTagListCheck.removeTag(tag);
    }
}
