package com.momrach.basichandler;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.ref.WeakReference;

import androidx.annotation.MainThread;

public class WorkerTrhead extends Thread {
    public Handler mHandler;
    private Handler mUIHandler;
    private WeakReference<MainActivity>  theActivity;
    int mDelay = 10000; //10 seconds
    static int counter;


    public WorkerTrhead(MainActivity theActivity) {
        this.theActivity = new WeakReference<MainActivity>(theActivity);
        this.mUIHandler = new Handler(Looper.getMainLooper());
    }

    private class MyHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg) {
            if (theActivity.get() != null){
                if (msg.what == 0) {
                    counter++;
                    doLongRunningOperation();
                    mUIHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            TextView tvText;
                            tvText = (TextView) theActivity.get().findViewById(R.id.tvText);
                            tvText.setText("Run " + counter + " times.");
                        }
                    });
                }
            }
            else{
                mUIHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        TextView tvText;
                        tvText = (TextView) theActivity.get().findViewById(R.id.tvText);
                        tvText.setText("The message was lost, try again, please.");
                    }
                });
            }

        }
    }

    @SuppressLint("HandlerLeak")
    public void run() {
        counter=0;
        Looper.prepare();

        mHandler = new MyHandler();
        /*
        {  // Salta un warning (quitar el supresslint para verlo) que no tiene efecto, leer ayuda del warning, aqui el looper no es el del UI thread.
            public void handleMessage(Message msg) {
                if (theActivity.get() != null){
                     if (msg.what == 0) {
                         counter++;
                         doLongRunningOperation();
                         mUIHandler.post(new Runnable() {
                             @Override
                             public void run() {
                                 TextView tvText;
                                 tvText = (TextView) theActivity.get().findViewById(R.id.tvText);
                                 tvText.setText("Run " + counter + " times.");
                             }
                         });
                     }
                 }

            }
        };*/
        Looper.loop();
    }

    private void doLongRunningOperation(){
        //Sleeps total 6xmDelay = 1 minute.
        for (int i=0; i<1; i++) {
            sleep();
        }
    }

    private void sleep() {
        try {
            Thread.sleep(mDelay);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
