package com.sheep.gamegroup.absBase;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.view.dialog.DialogLoading;

import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by kemllor on 2017/12/15.
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected DialogLoading dialogLoading;
    public Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        unbinder = ButterKnife.bind(this);
        initView();
        initListener();
        initData();
    }
    public void reBindView(){
        if(unbinder != null){
            try {
                unbinder.unbind();
            } catch (Exception e) {
                e.printStackTrace();
            }
            unbinder = null;
        }
        unbinder = ButterKnife.bind(this);
    }
    public boolean isShowing() {
        return dialogLoading != null && dialogLoading.getAlertDialog() != null && dialogLoading.getAlertDialog().isShowing();
    }

    public void showProgress() {
        hideProgress();
        dialogLoading = DialogLoading.showDialog(this);
    }

    protected void showProgress(boolean cancel) {
        hideProgress();
        dialogLoading = DialogLoading.showDialog(this, cancel);
    }


    public void hideProgress() {
        if (isShowing()) {
            try {
                dialogLoading.getAlertDialog().dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        final Object action = DataUtil.getInstance().getAction(getClass().getSimpleName());
        if (action != null) {
            //延时一会儿，不然有可能操作会无效
            Observable.just(1).delay(50L, TimeUnit.MILLISECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<Integer>() {
                        @Override
                        public void call(Integer integer) {
                            doNextAction(action);
                        }
                    });
        }
    }

    protected abstract int getLayoutId();

    public abstract void initView();

    public void initListener() {

    }

    public void initData() {

    }

    public void doNextAction(Object action) {

    }

    @Override
    protected void onDestroy() {
        try {
            super.onDestroy();
            if (this.unbinder != null) {
                this.unbinder.unbind();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
