package ai.pai.lensman.upload;

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;

public class UploadActivity extends AppCompatActivity implements UploadContract.UploadView{

    private TextView btStatusTextView;
    private ImageView btStatusImageView;
    private View noDataLayout;
    private SessionRecyclerAdapter adapter;
    private RecyclerView sessionsRecyclerView;
    private UploadContract.UploadPresenter presenter;

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

        presenter = new UploadPresenterImpl(this,this);
        noDataLayout = findViewById(R.id.icon_no_data);
        btStatusImageView = (ImageView)findViewById(R.id.iv_bt_status);
        btStatusTextView = (TextView)findViewById(R.id.tv_bt_status);
        sessionsRecyclerView = (RecyclerView)findViewById(R.id.recycler_view_sessions);
        adapter = new SessionRecyclerAdapter(this);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,2,LinearLayoutManager.VERTICAL,false);
        sessionsRecyclerView.setLayoutManager(layoutManager);
        sessionsRecyclerView.setAdapter(adapter);
        findViewById(R.id.iv_briefs).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                presenter.jumpToBriefs();
            }
        });

        findViewById(R.id.iv_add_session).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                presenter.jumpToNewSession();
            }
        });

        btStatusImageView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                presenter.checkBTStatus();
            }
        });

    }

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

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

    @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);
            }
        });
    }

}
