package com.xwkj.jcjcgz.mvp.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xwkj.jcjcgz.R;
import com.xwkj.jcjcgz.app.view.FlikerProgressBar;
import com.xwkj.jcjcgz.di.component.DaggerAppDetailComponent;
import com.xwkj.jcjcgz.mvp.contract.AppDetailContract;
import com.xwkj.jcjcgz.mvp.presenter.AppDetailPresenter;
import com.yaoxiaowen.download.DownloadConstant;
import com.yaoxiaowen.download.DownloadHelper;
import com.yaoxiaowen.download.FileInfo;
import com.zkjc.common.base.ZkjcBaseActivity;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/05/2024 16:24
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class AppDetailActivity extends ZkjcBaseActivity<AppDetailPresenter> implements AppDetailContract.View, View.OnClickListener {

    private static final String ACTION = "download_helper_action";

    private String apkName = "测试.apk";
    @BindView(R.id.app_detail_progress)
    FlikerProgressBar progressBar;
    private File apkFile;
    private File dir;
    private boolean isDownload;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerAppDetailComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_app_detail; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        progressBar.setOnClickListener(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION);
        registerReceiver(receiver, filter);
        apkFile = new File(getDir(), apkName);
    }
    private File getDir(){
        if (dir!=null && dir.exists()){
            return dir;
        }

        dir = new File(getExternalCacheDir(), "download");
        if (!dir.exists()){
            dir.mkdirs();
        }
        return dir;
    }
    public void getAppDetail(View view) {
        Map<String, String> map = new HashMap<>();
        map.put("id", "1");
        mPresenter.getAppDetail(map);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onGetAppDetailSuccess(String toString) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.app_detail_progress:
                if (isDownload) {
                    DownloadHelper helper = DownloadHelper.getInstance();
                    helper.pauseTask("http://118.195.196.59:9000/jdrh/upload/20240704/2fe3c1a6992b109e20efdc991fd9a2cf.apk", apkFile, ACTION).submit(this);
                    progressBar.toggle();
                } else {
                    DownloadHelper helper = DownloadHelper.getInstance();
                    helper.addTask("http://118.195.196.59:9000/jdrh/upload/20240704/2fe3c1a6992b109e20efdc991fd9a2cf.apk", apkFile, ACTION).submit(this);
                    progressBar.toggle();
                }
                isDownload = !isDownload;
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("eee", "onReceive is run !!!!!");
            FileInfo firstFileInfo = (FileInfo) intent.getSerializableExtra(DownloadConstant.EXTRA_INTENT_DOWNLOAD);
            float pro = (float) (firstFileInfo.getDownloadLocation()*1.0/ firstFileInfo.getSize());
            int progress = (int)(pro*100);
            progressBar.setProgress(progress);
        }
    };
}
