package com.duben.library.base;

import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;

import com.duben.microtribe.R;


/**
 * @author assen
 * @date 2019/7/4
 */
public abstract class TransitionActivity extends AppCompatActivity {

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        if (toggleIsBack2Left()) {
            setTransitionAnimation(false);
        } else {
            setTransitionAnimation(true);
        }
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        /*
            judge is the previous page from left to right
         */
        if (toggleIsBack2Left()) {
            setTransitionAnimation(false);
        } else {
            setTransitionAnimation(true);
        }
    }

    @Override
    public void finish() {
        super.finish();
        if (toggleIsBack2Left()) {
            setTransitionAnimation(true);
        } else {
            setTransitionAnimation(false);
        }
    }

    /**
     * finish from left
     */
    protected void finish2Left() {
        super.finish();
        setTransitionAnimation(false);
    }

    /**
     * finish from right
     */
    protected void finish2Right() {
        super.finish();
        setTransitionAnimation(true);
    }

    /**
     * finish normal
     */
    protected void finish2Normal() {
        super.finish();
    }

    /**
     * toggle is back to left
     */
    protected abstract boolean toggleIsBack2Left();

    /**
     * change activity transition animation
     *
     * @param newActivityIn
     */
    private void setTransitionAnimation(boolean newActivityIn) {
        if (newActivityIn) {
            // 新页面从右边进入  <---
            overridePendingTransition(R.anim.right_in,
                    R.anim.right_out);
        } else {
            // 上一个页面从左边进入  --->
            overridePendingTransition(R.anim.left_in,
                    R.anim.left_out);
        }
    }


}
