package com.eltech.soceltech.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.eltech.soceltech.App;
import com.eltech.soceltech.R;
import com.eltech.soceltech.data.model.entity.DaoSession;
import com.eltech.soceltech.data.model.entity.SocTest;
import com.eltech.soceltech.ui.adapters.TestRecyclerViewAdapter;
import com.eltech.soceltech.ui.fragments.TestViewFragment;
import com.eltech.soceltech.ui.listeners.RecyclerItemClickListener;

import java.util.List;

import javax.inject.Inject;

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

/**
 * @author komdosh
 *         created on 22.05.17.
 */

public class MainActivity extends AppCompatActivity {

	@BindView(R.id.testsRecyclerView)
	RecyclerView recyclerView;

	@Inject
	DaoSession daoSession;

	List<SocTest> socTests;

	private TestRecyclerViewAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ButterKnife.bind(this);
		App.getComponent().inject(this);

		Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
		setSupportActionBar(toolbar);

		setupRecyclerView();
	}

	@OnClick(R.id.fab)
	public void setupFabClick(){
		Intent i = new Intent(MainActivity.this, AddTestActivity.class);
		startActivity(i);
	}

	private void setupRecyclerView() {
		LinearLayoutManager layoutManager = new LinearLayoutManager(this);
		recyclerView.setLayoutManager(layoutManager);
		recyclerView.addItemDecoration(new DividerItemDecoration(this, layoutManager
				.getOrientation()));
		recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
			@Override
			public void onItemClick(View view, int position) {
				Intent intent = new Intent(MainActivity.this, TestViewActivity.class);
				intent.putExtra(TestViewActivity.ARG_TEST_ID, socTests.get(position).getId());
				startActivity(intent);
			}
		}));

		socTests = daoSession.getSocTestDao().loadAll();

		adapter = new TestRecyclerViewAdapter(socTests);
		recyclerView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getGroupId()) {
			case R.id.delete:
				deleteItem(item.getItemId());
				return true;
			case R.id.edit:
				Intent i = new Intent(MainActivity.this, AddTestActivity.class);
				i.putExtra(AddTestActivity.ARG_TEST_ID, socTests.get(item.getItemId()).getId());
				startActivity(i);
				return true;
			default:
				return super.onContextItemSelected(item);
		}
	}

	@Override
	public void onResume(){
		super.onResume();
		adapter.notifyDataSetChanged();
	}

	private void deleteItem(int itemId){
		long id = socTests.get(itemId).getId();
		socTests.remove(itemId);
		adapter.notifyDataSetChanged();
		daoSession.getSocTestDao().deleteByKey(id);
	}
}