package com.xplora.xpsettings.Activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xplora.xpsettings.R;


public class SoftwareUpdatingActivity extends BaseActivity {
    int mViewType = 1;
    double mProgress = 0;

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

        initData();
        initView();
        start();
    }

    private void initData() {
        mProgress = 10.;
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        RelativeLayout mDownloadView = findViewById(R.id.download);
        RelativeLayout mInstallView = findViewById(R.id.install);
        RelativeLayout mRetryView = findViewById(R.id.retry);

        mDownloadView.setVisibility(mViewType == 1 ? View.VISIBLE : View.GONE);
        mInstallView.setVisibility(mViewType == 2 ? View.VISIBLE : View.GONE);
        mRetryView.setVisibility(mViewType == 3 ? View.VISIBLE : View.GONE);

        if (mViewType == 1) {
            TextView mDownloadText = findViewById(R.id.downloadText);
            TextView mDownloadProgressText = findViewById(R.id.downloadProgressText);
            ProgressBar mDownloadProgressBar = findViewById(R.id.downloadProgressBar);
            if (mDownloadText != null) {
                mDownloadText.setText(R.string.software_downloading);
            }
            if (mDownloadProgressText != null) {
                @SuppressLint("DefaultLocale") String s = String.format("%.1f", mProgress);
                mDownloadProgressText.setText(s + " %");
            }
            if (mDownloadProgressBar != null) {
                mDownloadProgressBar.setProgress((int) Math.ceil(mProgress));
            }
        } else if (mViewType == 2) {
            TextView mInstallText = findViewById(R.id.installText);
            TextView mInstallProgressText = findViewById(R.id.installProgressText);
            TextView mInstallHintText = findViewById(R.id.installHintText);
            ProgressBar mInstallProgressBar = findViewById(R.id.installProgressBar);
            if (mInstallText != null) {
                mInstallText.setText(R.string.software_install);
            }
            if (mInstallProgressText != null) {
                @SuppressLint("DefaultLocale") String s = String.format("%.1f", mProgress);
                mInstallProgressText.setText(s + " %");
            }
            if (mInstallHintText != null) {
                mInstallHintText.setText(R.string.software_install_hint);
            }
            if (mInstallProgressBar != null) {
                mInstallProgressBar.setProgress((int) Math.ceil(mProgress));
            }
        } else if (mViewType == 3) {
            TextView mRetryText = findViewById(R.id.retryText);
            Button mRetryButton = findViewById(R.id.retryButton);

            if (mRetryText != null)
                mRetryText.setText(R.string.software_failed);
            if (mRetryButton != null)
                mRetryButton.setText(R.string.button_retry);

            assert mRetryButton != null;
            mRetryButton.setOnClickListener(v -> finish());
        }
    }

    private void start() {
        switch (mViewType) {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
        }
    }

}