package ai.pai.lensman.session;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

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

public class SessionActivity extends BaseActivity implements SessionContract.View {

    @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.Presenter presenter;

    private static final int JOIN_REQUEST_CODE = 3002;

    private ItemTouchHelper.Callback mCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
            ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT) {

        @Override
        public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
                final float alpha = 1 - Math.abs(dX) / (float)viewHolder.itemView.getWidth();
                viewHolder.itemView.setAlpha(alpha);
                viewHolder.itemView.setTranslationX(dX);
            }
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();
            presenter.swipeToDeletePhoto(position);
            adapter.removePhotoAtIndex(position);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_session);
        unbinder = ButterKnife.bind(this);
        sessionBean =(SessionBean)getIntent().getSerializableExtra("session");
        presenter = new SessionPresenter(sessionBean,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);

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(mCallback);
        itemTouchHelper.attachToRecyclerView(photosRecyclerView);

    }

    @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 synchronized void addNewPhoto(final PhotoBean bean) {
        adapter.addPhotoBean(bean);
        photosRecyclerView.smoothScrollToPosition(0);
    }

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

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

    @Override
    public void showToast(String toast) {
        Toast.makeText(this,toast,Toast.LENGTH_SHORT).show();
    }

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

}
