package com.example.cloudmusic;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;

public class ToastActivity extends Activity {
    private Button on;
    private int number = 0;
    private ProgressDialog progressDialog;
    private Handler mMainHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);

        on = findViewById(R.id.btn_on);
        on.setOnClickListener(v -> {
            show();
            showProgress();
        });
        mMainHandler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        if ((boolean)msg.obj){
                            Toast.makeText(ToastActivity.this, "上传完成", Toast.LENGTH_SHORT).show();
                            progressDialog.cancel();
                        }
                }
            }
        };
    }

    private void showProgress() {
        Handler handler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        Toast.makeText(ToastActivity.this, "到我这里了", Toast.LENGTH_SHORT).show();
                        System.out.println((int)msg.obj );
                        if ((int)msg.obj >= 100){
                            mMainHandler.sendMessage(mMainHandler.obtainMessage(1,true));
                        }
                }
            }
        };
        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                while(number <= 100){
                    number += 1;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            progressDialog.setProgress(number);
                        }
                    });
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //Toast.makeText(ToastActivity.this, "上传成功", Toast.LENGTH_SHORT).show();

                Message msg = new Message();
                msg.what = 1;
                msg.obj = number;
                handler.sendMessage(msg);
                number = 0;
                Looper.loop();
            }
        }).start();
    }

    private void show() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMax(100);
        progressDialog.setProgress(0);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setTitle("请稍等");
        progressDialog.setMessage("当前上传进度");
        progressDialog.show();
    }
}
