package com.cooleshow.student.widgets.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.utils.GlideUtils;
import com.cooleshow.base.utils.MyFileUtils;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.student.R;
import com.cooleshow.student.helper.ShareHelper;
import com.cooleshow.student.ui.web.HtmlActivity;
import com.cooleshow.usercenter.helper.UserHelper;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.io.File;

import androidx.annotation.NonNull;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * Author by pq, Date on 2022/9/21.
 */
public class LiveRoomShareDialog extends Dialog {

    private ImageView mIvAvatarAnchor;
    private ImageView mIvAvatarOwner;
    private TextView mTvShareTitle;
    private TextView mTvSharerName;
    private ImageView mIvCode;
    private TextView mTvSave;
    private TextView mTvShare;
    private boolean isNeedCreateQRCode = true;
    private View mShareView;
    private OnEventListener mOnEventListener;
    private View mCsRoot;

    public LiveRoomShareDialog(@NonNull Context context) {
        super(context, R.style.DialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_live_share_layout);
        mShareView = findViewById(R.id.cs_share_view);
        mCsRoot = findViewById(R.id.cs_root);
        mIvAvatarAnchor = findViewById(R.id.iv_avatar_anchor);
        mIvAvatarOwner = findViewById(R.id.iv_avatar_owner);
        mTvShareTitle = findViewById(R.id.tv_share_title);
        mTvSharerName = findViewById(R.id.tv_sharer_name);
        mIvCode = findViewById(R.id.iv_code);
        mTvSave = findViewById(R.id.tv_save);
        mTvShare = findViewById(R.id.tv_share);

        mTvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmapByView = ShareHelper.createBitmapByView(mShareView);
                if (bitmapByView != null) {
                    ShareHelper.saveImg(getContext(), bitmapByView, new ShareHelper.ResultCallBack() {
                        @Override
                        public void onResult(boolean isSuccess) {
                            if (isSuccess) {
                                ToastUtil.getInstance().showShort("保存成功");
                                dismiss();
                            } else {
                                ToastUtil.getInstance().showShort("保存失败");
                            }
                        }
                    });
                }
            }
        });

        mTvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmapByView = ShareHelper.createBitmapByView(mShareView);
                //此处待优化,最好子线程去保存
                File file = ShareHelper.saveImageToGallery(bitmapByView);
                if (bitmapByView != null) {
                    if (mOnEventListener != null) {
                        mOnEventListener.onShare(file);
                    }
                }
            }
        });

        String userName = UserHelper.getUserName();
        String userAvatar = UserHelper.getUserAvatar();
        GlideUtils.INSTANCE.loadImage(getContext(), userAvatar, mIvAvatarOwner, R.drawable.icon_student_default_head);
        mTvSharerName.setText(String.format("%s 为您推荐", userName));
    }

    private void share() {

    }

    public void setData(String liveAnchorAvatar, String liveAnchorName, String liveRoomId) {
        //直播老师头像
        GlideUtils.INSTANCE.loadImage(getContext(), liveAnchorAvatar, mIvAvatarAnchor, R.drawable.icon_student_default_head);
        mTvShareTitle.setText(String.format("%s正在直播中！去围观吧～", liveAnchorName));
        createQRCode(liveRoomId);
    }

    private void createQRCode(String roomId) {
        if (!isNeedCreateQRCode) {
            return;
        }
        String userId = UserHelper.getUserId();
        String url = String.format(WebConstants.SHARE_LIVE_URL, roomId,userId);
        Observable.create(new ObservableOnSubscribe<Bitmap>() {
            @Override
            public void subscribe(@io.reactivex.rxjava3.annotations.NonNull ObservableEmitter<Bitmap> emitter) throws Throwable {
                Bitmap bitmap = CodeUtils.createImage(url, SizeUtils.dp2px(89), SizeUtils.dp2px(89), null);
                emitter.onNext(bitmap);
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Bitmap>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull Bitmap bitmap) {
                        if (bitmap != null && mIvCode != null) {
                            isNeedCreateQRCode = false;
                            mIvCode.setImageBitmap(bitmap);
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public View getRootView() {
        return getWindow().getDecorView();
    }

    public void setOnEventListener(OnEventListener onEventListener) {
        this.mOnEventListener = onEventListener;
    }

    public interface OnEventListener {
        void onShare(File file);
    }

}
