package ai.pai.lensman.upload;

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

import com.android.common.utils.LogHelper;

import java.util.ArrayList;

import ai.pai.lensman.R;
import ai.pai.lensman.base.BaseActivity;
import ai.pai.lensman.bean.SessionBean;
import ai.pai.lensman.briefs.BriefsActivity;
import ai.pai.lensman.session.SessionActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UploadActivity extends BaseActivity implements UploadContract.View {

    @BindView(R.id.tv_box_status) TextView boxStatusTextView;
    @BindView(R.id.iv_box_status) ImageView boxStatusImageView;
    @BindView(R.id.icon_no_data) android.view.View noDataLayout;
    @BindView(R.id.iv_add_session) ImageView addSessionBtn;
    @BindView(R.id.recycler_view_sessions) RecyclerView sessionsRecyclerView;
    private SessionRecyclerAdapter adapter;
    private UploadContract.Presenter presenter;

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

        presenter = new UploadPresenter(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(){
        jumpToSelectedSession( presenter.createNewSession());
    }

    @OnClick(R.id.iv_box_status)
    void checkBoxStatus(){

    }

    @Override
    public void showBoxDisconnectedView() {
        boxStatusTextView.setText(R.string.bt_disconnected);
        boxStatusImageView.setImageResource(R.drawable.wifi_disconnect);
    }

    @Override
    public void showBoxConnectedView() {
        boxStatusTextView.setText(R.string.bt_connected);
        boxStatusImageView.setImageResource(R.drawable.wifi_connect);
    }

    @Override
    public void showEmptyView() {
        sessionsRecyclerView.setVisibility(android.view.View.GONE);
        noDataLayout.setVisibility(android.view.View.VISIBLE);
    }

    @Override
    public void showSessionViews() {
        noDataLayout.setVisibility(android.view.View.GONE);
        sessionsRecyclerView.setVisibility(android.view.View.VISIBLE);
    }

    @Override
    public void updateSessionUploadViewAt(int position) {
        LogHelper.d("czy","updateSessionUploadViewAt "+position);
        adapter.notifyItemChanged(position);
    }

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

    @Override
    public void setNewSessionBtnEnabled(boolean isEnabled) {
        addSessionBtn.setEnabled(isEnabled);
    }


    private void jumpToSelectedSession(SessionBean sessionBean) {
        Intent intent = new Intent(this, SessionActivity.class);
        intent.putExtra("session",sessionBean);
        startActivity(intent);
    }

}
