package ai.pai.lensman.session;

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

import ai.pai.lensman.R;
import ai.pai.lensman.upload.SessionBean;

public class SessionActivity extends AppCompatActivity implements SessionContract.SessionView,View.OnClickListener {

    private View noPhotoLayout;
    private RecyclerView photosRecyclerView;
    private TextView titleTextView;
    private ImageView scanImageView;
    private ImageView qrcodeImageView;
    private ImageButton sessionCompleteBtn;
    private PhotoRecyclerAdapter adapter;

    private SessionContract.SessionPresenter presenter;

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

        presenter = new SessionPresenterImpl(this,this);

        SessionBean sessionBean =(SessionBean)getIntent().getSerializableExtra("session");
        titleTextView = (TextView)findViewById(R.id.title_bar_middle_txt);
        titleTextView.setText(getString(R.string.scene)+sessionBean.sessionSeq);
        noPhotoLayout = findViewById(R.id.icon_no_data);
        photosRecyclerView = (RecyclerView)findViewById(R.id.recycler_view_photos);
        adapter = new PhotoRecyclerAdapter(this);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        photosRecyclerView.setLayoutManager(layoutManager);
        photosRecyclerView.setAdapter(adapter);
        scanImageView = (ImageView)findViewById(R.id.iv_scan);
        qrcodeImageView = (ImageView)findViewById(R.id.iv_qrcode);
        sessionCompleteBtn = (ImageButton)findViewById(R.id.btn_session_complete);

        scanImageView.setOnClickListener(this);
        qrcodeImageView.setOnClickListener(this);
        sessionCompleteBtn.setOnClickListener(this);
        findViewById(R.id.title_bar_back_layout).setOnClickListener(this);
    }

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

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

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.title_bar_back_layout:
                finish();
                break;
            case R.id.iv_qrcode:
                presenter.showQRCodeForSession();
                break;
            case R.id.iv_scan:
                presenter.scanToFetchSessionQR();
                break;
            case R.id.btn_session_complete:
                presenter.onSessionComplete();
                break;
        }
    }

    @Override
    public void addNewPhoto(final PhotoBean bean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.addPhotoBean(bean);
                photosRecyclerView.smoothScrollToPosition(0);
            }
        });

    }

    @Override
    public void showPhotoRecyclerView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                noPhotoLayout.setVisibility(View.GONE);
                photosRecyclerView.setVisibility(View.VISIBLE);
            }
        });
    }

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

    }
}
