package com.jld.vod.view.widget;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;

import com.jld.vod.R;
import com.jld.vod.interfaces.IDownloadCallBack;
import com.jld.vod.model.bean.DownloadApkBean;
import com.jld.vod.utils.http.OkHttpUtils;
import com.jld.vod.utils.SilentInstall;

import java.io.File;

/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2019/10/29 16:54
 */
public class UpdateVersionDialog extends DialogFragment {

    private final static String DOWNLOAD_KEY = "download_bean";
    private DownloadApkBean downloadBean;
    private final static String TAG = "UpdateVersionDialog";
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if(arguments != null)
        {
            downloadBean = (DownloadApkBean) arguments.getSerializable(DOWNLOAD_KEY);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.update_dialog,container,false);
        BingdEvent(view);
        return view;
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    private void BingdEvent(View view) {
        TextView tvtitle = view.findViewById(R.id.tv_title);
        TextView tvcontent = view.findViewById(R.id.tv_content);

        tvtitle.setText(downloadBean.title);
        tvcontent.setText(downloadBean.content);

        final TextView btupdate = view.findViewById(R.id.bu_update);

        view.setEnabled(false);
        File targetFile = new File(getActivity().getCacheDir(),"target.apk");

        OkHttpUtils.getInstance().download(downloadBean.url, targetFile, new IDownloadCallBack() {
                    @Override
                    public void success(File apkfile) {
                        view.setEnabled(true);
                        Log.i(TAG,"apkfile="+apkfile.getAbsolutePath());
                        dismiss();
                        SilentInstall.install(apkfile.getAbsolutePath());
                    }
                    @Override
                    public void progress(int progress) {
                        Log.i(TAG,"progress="+progress);
                        btupdate.setText(progress+" %");
                    }
                    @Override
                    public void failed(Throwable throwable) {
                        Toast.makeText(getActivity(),"下载apk失败",Toast.LENGTH_LONG).show();
                        view.setEnabled(true);
                    }
        });
    }

    public static void show(FragmentActivity activity , DownloadApkBean bean){
        Bundle bundle = new Bundle();
        bundle.putSerializable(DOWNLOAD_KEY,bean);

        UpdateVersionDialog updateVersionDialog = new UpdateVersionDialog();
        updateVersionDialog.setArguments(bundle);
        updateVersionDialog.show(activity.getSupportFragmentManager(),"updateVersionShowDialog");
    }
}
