package com.qhclh.ytzh.tasks;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;


import com.qhclh.ytzh.utils.DialogUtil;

import java.util.concurrent.Callable;

public abstract class CallableTask implements Callable<Object> {
    final String tag = "CallableTask";

    Boolean mIsRunning = false;

    protected abstract void successUI();

    void failUI(Exception e) {
        Log.e(tag, e.getMessage(), e);
        if(e.getMessage().contains("Unauthorized"))
        {
            return;
        }
        if(e.getMessage().contains("Socket"))
        {
            return;
        }
        if(e.getMessage().contains("cannot"))
        {
            return;
        }

        DialogUtil.AutoHiddenAlert(mContext,e.getMessage());
//        DialogUtil.Alert(mContext, e.getMessage());
    }

    Context mContext;
    ProgressDialog mDialog;
    String mMessage;

    public CallableTask(Context context, String message) {
        mContext = context;
        mMessage = message;
    }

    public final void execute() {
        if (mIsRunning) {
            Log.w(tag, "任务重复执行");
            return;
        }
        new Task().execute();
    }

    class Task extends AsyncTask<Void, Void, Void> {
        Exception mException;

        @Override
        protected Void doInBackground(Void... params) {
            try {
                mIsRunning = true;
                call();
            } catch (Exception ex) {
                mException = ex;
                cancel(false);
            }
            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
            endExecute();
            failUI(mException);
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            endExecute();
            successUI();
        }


        public void endExecute() {
//            if (mDialog != null && isValidContext(mContext) && mDialog.isShowing()) {
            if (mDialog != null) {
                mDialog.dismiss();
            }
            mIsRunning = false;

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
//            if (isValidContext(mContext)) {
                mDialog = ProgressDialog.show(mContext, "请稍等", mMessage, true);
//            }
        }

    }

//    private boolean isValidContext (Context c){
//        Activity a = (Activity)c;
//        if (a.isDestroyed() || a.isFinishing()){
//            return false;
//        }else{
//            return true;
//        }
//    }


}
