package com.cooleshow.teacher.widgets;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cooleshow.base.common.BaseApplication;
import com.cooleshow.base.utils.GlideUtils;
import com.cooleshow.base.utils.PopupUtil;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.widgets.dialog.CommonDialog;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.MessageBoxAdapter;
import com.cooleshow.teacher.adapter.ScoreShareListAdapter;
import com.cooleshow.teacher.bean.MusicSheetShareBean;
import com.cooleshow.usercenter.utils.SavePicUtil;
import com.uuzuche.lib_zxing.activity.CodeUtils;

/**
 * 创建日期：2022/5/24 15:22
 *
 * @author Ryan
 * 类说明：
 */
public class DialogUtils {
    public interface ActionDialogCallback {
        /**
         * Called when left Button is click
         */
        void onConfirmClick();

        /**
         * Called when left Button is click
         */
        void onCancelClick();

    }


    public interface ActionShareCallback {
        void onShareClick(Bitmap bitmap);
    }

    public static void showScoreShare(Context context, MusicSheetShareBean data, ActionShareCallback callback) {
        LinearLayout.LayoutParams pm = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.Dialog);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_score_share, null);//这里的R.layout.alertdialog即为你自定义的布局文件
        TextView tv_teacher_name = view.findViewById(R.id.tv_teacher_name);
        if (!TextUtils.isEmpty(data.url)) {
            Bitmap bitmap = CodeUtils.createImage(data.url, 400, 400, BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
            ImageView im_qr_code = view.findViewById(R.id.im_qr_code);
            if (bitmap != null) {
                im_qr_code.setImageBitmap(bitmap);
            }
        }
        tv_teacher_name.setText(data.name);
        ImageView im_header = view.findViewById(R.id.im_header);
        GlideUtils.INSTANCE.loadImage(context, data.avatar, im_header, R.drawable.icon_teacher_default_head);
        TextView tv_update_count = view.findViewById(R.id.tv_update_count);
        tv_update_count.setText("我在酷乐秀上传了" + data.musicSheetCount + "支乐谱");
        TextView tv_save = view.findViewById(R.id.tv_save);
        TextView tv_share = view.findViewById(R.id.tv_share);
        ConstraintLayout cl_content = view.findViewById(R.id.cl_content);
        RecyclerView rv_list = view.findViewById(R.id.rv_list);
        RelativeLayout rl_all = view.findViewById(R.id.rl_all);
        if (data.musicSheetCount == 0) {
            rv_list.setVisibility(View.GONE);
        } else {
            rv_list.setVisibility(View.VISIBLE);
            LinearLayoutManager manager = new LinearLayoutManager(context);
            rv_list.setLayoutManager(manager);
            ScoreShareListAdapter scoreShareListAdapter = new ScoreShareListAdapter(data.musicSheetVoList);
            rv_list.setAdapter(scoreShareListAdapter);
        }

        final AlertDialog mAlertDialog = builder.create();
        mAlertDialog.getWindow().setBackgroundDrawableResource(com.cooleshow.base.R.color.transparent);
        mAlertDialog.setCanceledOnTouchOutside(true);
        mAlertDialog.setCancelable(true);
        mAlertDialog.show();

        mAlertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        mAlertDialog.getWindow().setContentView(view, pm);
        tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int w = cl_content.getWidth();
                int h = cl_content.getHeight();
                Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
                Canvas c = new Canvas(bmp);
                cl_content.layout(0, 0, w, h);
                cl_content.draw(c);
                // 缩小图片
                Matrix matrix = new Matrix();
                matrix.postScale(1.0f, 1.0f); //长和宽放大缩小的比例
                bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                if (SavePicUtil.saveBitmap(BaseApplication.Companion.getContext(), bmp, System.currentTimeMillis() + "")) {
                    ToastUtil.getInstance().showShort("保存成功");
                } else {
                    ToastUtil.getInstance().showShort("保存失败");
                }
                mAlertDialog.dismiss();
            }
        });
        tv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAlertDialog.dismiss();
                if (null != callback) {
                    int w = cl_content.getWidth();
                    int h = cl_content.getHeight();
                    Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
                    Canvas c = new Canvas(bmp);
                    cl_content.layout(0, 0, w, h);
                    cl_content.draw(c);
                    // 缩小图片
                    Matrix matrix = new Matrix();
                    matrix.postScale(1.0f, 1.0f); //长和宽放大缩小的比例
                    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                    callback.onShareClick(bmp);
                }
            }
        });
        rl_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAlertDialog.dismiss();
            }
        });
    }


}
