package com.dm.wallpaper.board.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.widget.TextView;

import com.danimahardhika.android.helpers.core.ColorHelper;
import com.dm.wallpaper.board.R;
import com.dm.wallpaper.board.activities.callbacks.SplashScreenCallback;
import com.dm.wallpaper.board.activities.configurations.SplashScreenConfiguration;
import com.danimahardhika.android.helpers.core.utils.LogUtil;

import java.lang.ref.WeakReference;

import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

/*
 * Wallpaper Board
 *
 * Copyright (c) 2017 Dani Mahardhika
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public abstract class WallpaperBoardSplashActivity extends AppCompatActivity implements SplashScreenCallback {

    private AsyncTask mAsyncTask;

    private SplashScreenConfiguration mConfig;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mConfig = onInit();
        initBottomText();

        mAsyncTask = new SplashScreenLoader(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    @Override
    protected void onDestroy() {
        if (mAsyncTask != null) {
            mAsyncTask.cancel(true);
        }
        super.onDestroy();
    }

    private void initBottomText() {
        TextView splashTitle = findViewById(R.id.splash_title);
        if (splashTitle != null) {
            splashTitle.setText(mConfig.getBottomText());

            if (mConfig.getBottomTextColor() != -1) {
                splashTitle.setTextColor(mConfig.getBottomTextColor());
            } else {
                int color = ContextCompat.getColor(this, R.color.splashColor);
                splashTitle.setTextColor(ColorHelper.getBodyTextColor(color));
            }

            splashTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, mConfig.getBottomTextSize());
            splashTitle.setTypeface(mConfig.getBottomTextFont(this));
        }
    }

    private class SplashScreenLoader extends AsyncTask<Void, Void, Boolean> {

        private WeakReference<Context> context;

        private SplashScreenLoader(@NonNull Context context) {
            this.context = new WeakReference<>(context);
        }

        @Override
        protected Boolean doInBackground(Void... voids) {
            while (!isCancelled()) {
                try {
                    Thread.sleep(500);
                    return true;
                } catch (Exception e) {
                    LogUtil.e(Log.getStackTraceString(e));
                    return false;
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if (context.get() == null) return;
            if (context.get() instanceof Activity) {
                if (((Activity) context.get()).isFinishing()) return;
            }

            mAsyncTask = null;
            if (aBoolean) {
                Intent intent = new Intent(context.get(), mConfig.getMainActivity());
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            }
        }
    }
}

@@ -828,16 +828,55 @@
 erence;%0D
+%0Aimport java.util.concurrent.Executor;%0D
 %0A%0D%0Aimpor
@@ -2038,20 +2038,16 @@
 ncTask =
- new
  SplashS
@@ -2061,32 +2061,101 @@
 ader
+.with
 (this)
-.executeOnExecutor
+%0D%0A                .mainActivity(mConfig.getMainActivity())%0D%0A                .start
 (Asy
@@ -2468,24 +2468,56 @@
 cel(true);%0D%0A
+            mAsyncTask = null;%0D%0A
         %7D%0D%0A 
@@ -3276,16 +3276,23 @@
 private 
+static 
 class Sp
@@ -3400,16 +3400,56 @@
 ontext;%0D
+%0A        private Class%3C?%3E mainActivity;%0D
 %0A%0D%0A     
@@ -3570,32 +3570,468 @@
 ;%0D%0A        %7D%0D%0A%0D%0A
+        private SplashScreenLoader mainActivity(@NonNull Class%3C?%3E mainActivity) %7B%0D%0A            this.mainActivity = mainActivity;%0D%0A            return this;%0D%0A        %7D%0D%0A%0D%0A        private AsyncTask start(@NonNull Executor executor) %7B%0D%0A            return executeOnExecutor(executor);%0D%0A        %7D%0D%0A%0D%0A        private static SplashScreenLoader with(@NonNull Context context) %7B%0D%0A            return new SplashScreenLoader(context);%0D%0A        %7D%0D%0A%0D%0A
         @Overrid
@@ -4751,40 +4751,8 @@
 %0D%0A%0D%0A
-            mAsyncTask = null;%0D%0A
     
@@ -4835,27 +4835,16 @@
 get(), m
-Config.getM
 ainActiv
@@ -4846,18 +4846,16 @@
 Activity
-()
 );%0D%0A    
@@ -5001,16 +5001,154 @@
         
+if (context.get() instanceof Activity) %7B%0D%0A                    Activity activity = (Activity) context.get();%0D%0A                    activity.
 startAct
@@ -5179,16 +5179,29 @@
         
+    activity.
 override
@@ -5289,17 +5289,49 @@
     
+    
-finish();
+activity.finish();%0D%0A                %7D
 %0D%0A  

Update splash screen loader [ci skip]