package com.genlot.ninglottery.module.function.upgrade;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.genlot.nativesdk.model.Version;
import com.genlot.nativesdk.sender.TCPDownload;
import com.genlot.ninglottery.BuildConfig;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseFragment;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 升级界面
 * @date 2019/9/26
 */
public class UpGradeFragment extends BaseFragment {

    @OnClick(R.id.bt_confirm)
    public void upGrade() {
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_upgrade;
    }

    @Override
    public void onCreateViewFinished(Bundle state) {
        tvClientName.setText(BuildConfig.VERSION_NAME);
        tvClientCode.setText(String.valueOf(BuildConfig.VERSION_CODE));
        DownLoadTask task = new DownLoadTask();
        task.execute();
    }

    @SuppressLint("StaticFieldLeak")
    class DownLoadTask extends AsyncTask<Void, Void, Version> {

        @Override
        protected void onPreExecute() {
            showLoading();
        }

        @Override
        protected Version doInBackground(Void... voids) {
            return TCPDownload.downloadVersion();
        }

        @Override
        protected void onPostExecute(Version version) {
            dismissLoading();
            tvServerName.setText(version.name);
            tvServerCode.setText(String.valueOf(version.code));
            if (version.code <= BuildConfig.VERSION_CODE) {
                btConfirm.setEnabled(false);
            }
        }
    }


    @BindView(R.id.client_app_name)
    TextView tvClientName;
    @BindView(R.id.client_app_code)
    TextView tvClientCode;
    @BindView(R.id.sever_app_name)
    TextView tvServerName;
    @BindView(R.id.sever_app_code)
    TextView tvServerCode;
    @BindView(R.id.bt_confirm)
    Button btConfirm;
}
