package com.tofly.ghrq_route.common.ui.dialog;


import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatDialog;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.download.controller.DownloadResultCallback;
import com.tofly.ghrq_route.common.ui.activity.DownloadMapActivity;

/**
 * Created by lihualong on 2017/6/20.
 */

public class DownloadListDialog extends AppCompatDialog {
    private DownloadMapActivity downloadMapActivity;
    private int res;
    private View view;
    private TextView cityTv;
    private TextView downloadTv;
    private TextView cancleTv;
    private View.OnClickListener l;
    private String id;
    private String fileName;
    private DownloadResultCallback downloadResultCallback;

    public DownloadListDialog(DownloadMapActivity context, View view, int res) {
        super(context, R.style.dialog);
        this.downloadMapActivity = context;
        this.view = view;
        this.res = res;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
    }

    public void setTitle(String fileName) {
        cityTv.setText(fileName);
    }

    //    public void setSize(long size) {
//        String[] fileSize = FileUtil.fileSize(size);
//        downloadTv.setText("下载地图(" + fileSize[0] + fileSize[1] + ")");
//    }
    public void setSize(String size) {
        downloadTv.setText("下载地图(" + size + ")");
    }

    public void setParams(String id, String fileName) {
        this.id = id;
        this.fileName = fileName;
    }


    private void initListener() {
        if (l == null) {
            l = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int id = v.getId();
                    switch (id) {
                        case R.id.download:
                            startServiceToDownload();
                        case R.id.cancle:
                            DownloadListDialog.this.dismiss();
                            break;
                    }
                }
            };
        }
        cancleTv.setOnClickListener(l);
        downloadTv.setOnClickListener(l);
    }

    public static final String FILENAME = "filename";
    public static final String ID = "id";
    public static final String POSITION = "position";

    private void startServiceToDownload() {
        this.downloadResultCallback.startAndBind();
        downloadMapActivity.showDownloadDialog();
    }

    public void setStartDownloadServiceCallback(DownloadResultCallback downloadResultCallback) {
        this.downloadResultCallback = downloadResultCallback;
    }


    private void initView() {

//        setFocusable(true);
        View inflate = LayoutInflater.from(downloadMapActivity).inflate(res, null);
        cityTv = (TextView) inflate.findViewById(R.id.city);
        downloadTv = (TextView) inflate.findViewById(R.id.download);
        cancleTv = (TextView) inflate.findViewById(R.id.cancle);

        setContentView(inflate);
        Window window = getWindow();
        if (window != null) {
//            WindowManager.LayoutParams attributes = window.getAttributes();
//            attributes.width =inflate.getWidth()
            window.setGravity(Gravity.BOTTOM);
            window.setWindowAnimations(R.style.bottom_anim);
//            window.setWindowAnimations();

        }
        // 设置弹框的宽度为布局文件的宽
//        setWidth(ConstraintLayout.LayoutParams.MATCH_PARENT);
//        setHeight(ConstraintLayout.LayoutParams.WRAP_CONTENT);
//        showAtLocation(view, Gravity.NO_GRAVITY,0, toolbar.getHeight());
//        showAsDropDown(view);

    }
}
