package ai.pai.lensman.upload;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import ai.pai.lensman.R;
import ai.pai.lensman.briefs.BriefsActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UploadActivity extends AppCompatActivity implements UploadContract.UploadView{

    @BindView(R.id.tv_bt_status) TextView btStatusTextView;
    @BindView(R.id.iv_bt_status) ImageView btStatusImageView;
    @BindView(R.id.icon_no_data) View noDataLayout;
    @BindView(R.id.recycler_view_sessions) RecyclerView sessionsRecyclerView;
    private SessionRecyclerAdapter adapter;
    private UploadContract.UploadPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        ButterKnife.bind(this);

        presenter = new UploadPresenterImpl(this,this);
        adapter = new SessionRecyclerAdapter(this);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,2,LinearLayoutManager.VERTICAL,false);
        sessionsRecyclerView.setLayoutManager(layoutManager);
        sessionsRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        presenter.stop();
    }

    @OnClick(R.id.iv_briefs)
    void jumpToBriefs(){
        startActivity(new Intent(this, BriefsActivity.class));
    }

    @OnClick(R.id.iv_add_session)
    void jumpToNewSession(){
        presenter.jumpToNewSession();
    }

    @OnClick(R.id.iv_bt_status)
    void checkBTStatus(){
        presenter.checkBTStatus();
    }


    @Override
    public void addNewSessionView(SessionBean bean) {

    }

    @Override
    public void showBTDisconnectedView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                btStatusTextView.setText(R.string.bt_disconnected);
                btStatusImageView.setImageResource(R.drawable.bluetooth_disconnect);
            }
        });

    }

    @Override
    public void showBTConnectedView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                btStatusTextView.setText(R.string.bt_connected);
                btStatusImageView.setImageResource(R.drawable.bluetooth_connected);
            }
        });
    }

    @Override
    public void showEmptyView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                sessionsRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        });

    }

    @Override
    public void showSessionViews() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                noDataLayout.setVisibility(View.GONE);
                sessionsRecyclerView.setVisibility(View.VISIBLE);
            }
        });

    }

    @Override
    public void updateSessionUploadView(SessionBean bean) {

    }

    @Override
    public void refreshSessionViews(final ArrayList<SessionBean> sessionList) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.clear();
                adapter.addSessionBeans(sessionList);
            }
        });
    }

}
