/*
 * Copyright (c) 2019 The sky Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.virjar.xposed.rimet.ui.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sky.xposed.common.ui.util.LayoutUtil;
import com.sky.xposed.common.ui.util.ViewUtil;
import com.sky.xposed.common.ui.view.CommonFrameLayout;
import com.sky.xposed.common.ui.view.SimpleItemView;
import com.sky.xposed.common.util.DisplayUtil;
import com.sky.xposed.common.util.ToastUtil;
import com.virjar.xposed.rimet.ui.util.ActivityUtil;

/**
 * Created by sky on 2019/3/13.
 */
public class LoveDialog extends CommonDialog {

    private SimpleItemView sivAliPayLove;
    private SimpleItemView sivWeChatLove;

    @Override
    public void createView(CommonFrameLayout frameView) {

        LinearLayout.LayoutParams params = LayoutUtil.newWrapLinearLayoutParams();
        params.leftMargin = DisplayUtil.dip2px(getContext(), 15);
        params.topMargin = DisplayUtil.dip2px(getContext(), 5);
        params.bottomMargin = DisplayUtil.dip2px(getContext(), 5);

        TextView tvTip = new TextView(getContext());
        tvTip.setLayoutParams(params);
        tvTip.setText("这个世界有些人更需要你们的帮助！");
        tvTip.setTextSize(12);

        sivWeChatLove = ViewUtil.newSimpleItemView(getContext(), "微信公益");
        sivAliPayLove = ViewUtil.newSimpleItemView(getContext(), "支付宝公益");

        frameView.addContent(tvTip);
        frameView.addContent(sivWeChatLove);
        frameView.addContent(sivAliPayLove);
    }

    @Override
    protected void initView(View view, Bundle args) {
        super.initView(view, args);

        setTitle("公益");

        sivAliPayLove.setOnClickListener(v -> {
            // 启动支付宝
            startAliPayLove();
        });

        sivWeChatLove.setOnClickListener(v -> {
            // 启动微信
            startWeChatLove();
        });
    }

    /**
     * 启动微信公益
     */
    private void startWeChatLove() {

        Bundle bundle = new Bundle();
        bundle.putBoolean("LauncherUI.From.Biz.Shortcut", true);
        bundle.putString("app_shortcut_custom_id", "shortcut_c28a4ac297c2a53fc386336b79c38cc28fc29ac2b5c3acc38d");
        bundle.putString("LauncherUI.Shortcut.Username", "shortcut_c28a4ac297c2a53fc386336b79c38cc28fc29ac2b5c3acc38d");

        Intent intent = new Intent("com.tencent.mm.action.BIZSHORTCUT");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClassName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
        intent.putExtras(bundle);

        if (!ActivityUtil.startActivity(getContext(), intent)) {
            // 跳转失败
            ToastUtil.show("跳转到微信公益失败!");
        }
    }

    /**
     * 启动支付宝公益
     */
    private void startAliPayLove() {

        Bundle bundle = new Bundle();
        bundle.putBoolean("fromDesktop", true);


        String intentFullUrl = "intent://platformapi/startapp?saId=10000007&" +
                "clientVersion=3.7.0.0718&qrcode=https%3A%2F%2Fqr.alipay.com%2FFKX05428F2CIZP9P1BZN9A%3F_s" +   //这里的URLcode换成扫码得到的结果
                "%3Dweb-other&_t=" + System.currentTimeMillis() + "#Intent;" +
                "scheme=alipayqr;package=com.eg.android.AlipayGphone;end";
        try {
            Intent intent = Intent.parseUri(intentFullUrl, Intent.URI_INTENT_SCHEME);
            intent.putExtras(bundle);
            getActivity().startActivity(intent);
            if (!ActivityUtil.startActivity(getContext(), intent)) {
                // 跳转失败
                ToastUtil.show("跳转到支付宝公益失败!");
            }
        } catch (Exception e) {
            throw new RuntimeException("error", e);
        }
    }
}
