package com.ysnows.update;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.download.DownloadTask;
import com.ysnows.update.base.UpgradeActivity;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by shihuiyun
 * on 2020/6/3
 */
public class StormUpgradeActivity extends UpgradeActivity {


    private TextView tvVersion;
    private TextView tvFillsieze;
    private TextView tvUpdatetime;
    private TextView tvUpdatecontent;
    private LinearLayout buttonLeft;
    private TextView tvLeft;
    private LinearLayout buttonRight;
    private TextView tvRight;


    @Override
    protected void initData() {
        /*获取下载任务，初始化界面信息*/
        updateBtn(Beta.getStrategyTask());
        tvVersion.setText(Beta.getUpgradeInfo().title + getString(R.string.version_online));

        long fileSize = Beta.getUpgradeInfo().fileSize;
        String fill_size = getM(fileSize);

        tvFillsieze.setText(getString(R.string.package_size) + fill_size + getString(R.string.m));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(new Date(Beta.getUpgradeInfo().publishTime));

       // String time = times(String.valueOf(Beta.getUpgradeInfo().publishTime));
        tvUpdatetime.setText(getString(R.string.update_time) + time);
        tvUpdatecontent.setText(Beta.getUpgradeInfo().newFeature);


    }

    private void updateBtn(DownloadTask task) {

        switch (task.getStatus()) {
            case DownloadTask.INIT:

                break;
            case DownloadTask.DELETED:
                break;
            case DownloadTask.FAILED:
                tvRight.setText("立即升级");
                break;
            case DownloadTask.COMPLETE:
                tvRight.setText("安装");
                break;
            case DownloadTask.DOWNLOADING:
                tvRight.setText("暂停");
                break;
            case DownloadTask.PAUSED:
                tvRight.setText("继续下载");
                break;

        }
    }

    public String getM(long k) {

        try {
            double m = k / 1024.0 / 1024.0;
            DecimalFormat dfs = new DecimalFormat("0.0");
            String jingdu = dfs.format(m);
            return jingdu;

        } catch (Exception e) {
            return "";
        }
    }

    public String times(String time) {


        try {
            String ss = "";
            if (time.length() > 10) {
                ss = time.substring(0, 10);
            } else {
                ss = time;
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:io.agora.rtc.ss");
            Long lcc = java.lang.Long.valueOf(ss);
            String times = sdf.format(new Date(lcc * 1000L));
            return times;

        } catch (Exception e) {

        }
        return time;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_custom_upgrade;
    }

    @Override
    protected void initView() {
        tvVersion = findViewById(R.id.tv_version);
        tvFillsieze = findViewById(R.id.tv_fillsieze);
        tvUpdatetime = findViewById(R.id.tv_updatetime);
        tvUpdatecontent = findViewById(R.id.tv_updatecontent);
        buttonLeft = findViewById(R.id.button_left);
        tvLeft = findViewById(R.id.tv_left);
        buttonRight = findViewById(R.id.button_right);
        tvRight = findViewById(R.id.tv_right);


        buttonLeft.setOnClickListener(v -> {
            Beta.cancelDownload();
            finish();
        });

        //立即升级
        buttonRight.setOnClickListener(v -> {
            DownloadTask task = Beta.startDownload();
            updateBtn(task);
            if (task.getStatus() == DownloadTask.DOWNLOADING) {
                finish();
            }

        });

    }
}
