package ai.pai.lensman.session;

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

import ai.pai.lensman.R;
import ai.pai.lensman.base.BaseActivity;
import ai.pai.lensman.bean.PhotoBean;
import ai.pai.lensman.qrcode.QRCaptureActivity;
import ai.pai.lensman.bean.SessionBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SessionActivity extends BaseActivity implements SessionContract.SessionView {

    @BindView(R.id.icon_no_data) View noPhotoLayout;
    @BindView(R.id.title_bar_middle_txt) TextView titleTextView;
    @BindView(R.id.recycler_view_photos) RecyclerView photosRecyclerView;
    private PhotoRecyclerAdapter adapter;
    private  SessionBean sessionBean;
    private SessionContract.SessionPresenter presenter;

    private static final int JOIN_REQUEST_CODE = 3002;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_session);
        ButterKnife.bind(this);
        sessionBean =(SessionBean)getIntent().getSerializableExtra("session");
        presenter = new SessionPresenterImpl(this,sessionBean.sessionId,this);

        titleTextView.setText(getString(R.string.scene)+sessionBean.sessionSeq);
        adapter = new PhotoRecyclerAdapter(this);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        photosRecyclerView.setLayoutManager(layoutManager);
        photosRecyclerView.setAdapter(adapter);
    }

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

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

    @OnClick(R.id.iv_scan)
    void scanToFetchSessionQR(){
        Intent intent = new Intent(SessionActivity.this, QRCaptureActivity.class);
        startActivityForResult(intent, JOIN_REQUEST_CODE);
    }

    @OnClick(R.id.iv_qrcode)
    void showQRCodeForSession(){
        new SessionQRPopup(this, sessionBean.sessionId).showPopupWindow();
    }

    @OnClick(R.id.title_bar_back_layout)
    void backToMain(){
        finish();
    }

    @OnClick(R.id.btn_session_complete)
    void onSessionComplete(){
        finish();
    }

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

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if (resultCode == Activity.RESULT_OK) {
            if (data == null || data.getStringExtra("info") == null) {
                return;
            }
        }

    }
}
