package com.jachdev.consumerprotection.ui.bot;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

import android.os.Bundle;

import com.jachdev.commonlibs.base.BaseActivity;
import com.jachdev.commonlibs.validator.Validator;
import com.jachdev.commonlibs.widget.CustomEditText;
import com.jachdev.consumerprotection.R;
import com.jachdev.consumerprotection.data.BotMessage;
import com.jachdev.consumerprotection.data.Notification;
import com.jachdev.consumerprotection.ui.adapter.CommonAdaptor;
import com.jachdev.consumerprotection.ui.camera.CameraViewModel;

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

public class BotActivity extends BaseActivity {

    private BotViewModel botViewModel;

    private CommonAdaptor<BotMessage> adaptor;
    private List<BotMessage> botMessages = new ArrayList<>();

    @BindView(R.id.et_message)
    CustomEditText etMessage;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @Override
    protected int layoutRes() {
        return R.layout.activity_bot;
    }

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

        botViewModel = new ViewModelProvider(this).get(BotViewModel.class);

        observeViewModels();
        init();
    }

    @OnClick(R.id.btn_send)
    public void OnClickSend(){
        sendMessage();
    }

    private void init() {
        adaptor = new CommonAdaptor<>(BotActivity.this, botMessages, new CommonAdaptor.CommonAdaptorCallback() {
            @Override
            public void onView(int viewType, int position) {

            }
        });
        LinearLayoutManager ll = new LinearLayoutManager(BotActivity.this, LinearLayoutManager.VERTICAL,false);
        ll.setStackFromEnd(true);
        mRecyclerView.setLayoutManager(ll);
        mRecyclerView.setAdapter(adaptor);
    }

    private void observeViewModels() {
        botViewModel.getSenderMessage().observe(BotActivity.this, mSenderMessageObserve);
        botViewModel.getBotMessage().observe(BotActivity.this, mBotMessageObserve);
    }

    private void sendMessage() {
        if(Validator.isEditTextEmpty(etMessage, getString(R.string.please_enter_message))){
            botViewModel.sendMessage(etMessage.getTrimText());
            etMessage.setAnyText("");
        }
    }

    private Observer<? super BotMessage> mBotMessageObserve = new Observer<BotMessage>() {
        @Override
        public void onChanged(BotMessage message) {
            botMessages.add(message);

            adaptor.notifyDataSetChanged();
            mRecyclerView.scrollToPosition(botMessages.size() - 1);
        }
    };

    Observer<? super BotMessage> mSenderMessageObserve = new Observer<BotMessage>() {
        @Override
        public void onChanged(BotMessage message) {
            botMessages.add(message);

            adaptor.notifyDataSetChanged();
            mRecyclerView.scrollToPosition(botMessages.size() - 1);
        }
    };
}