package com.longtu.sample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.acegameproject.burstshooter.R;

import com.bh.sdk.Interface.LTBaseSDK;
import com.longtu.sdk.base.Listener.LTBaseSDKPushListener;
import com.longtu.sdk.base.bean.LTPushLocalData;
import com.longtu.sdk.base.utils.LTSDKUtils;
import com.longtu.sdk.utils.Log.Logs;


/**
 * 测试推送功能。
 * 作者：liqingshuo on 2020/8/3 11:25
 *
 * @author liqingshuo
 */
public class LTBase_Demo_PushActivity extends Activity implements View.OnClickListener {


    public Button mgetRegistrationID;
    public Button mSendLocalPush;

    public Button mRemoveLocalPush;
    public Button mRemoveAll;

    public Button mcancelPush;
    public Button mCancelAllPush;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.longtu_demo_activity_push);
        Logs.fi("LTPush", " MainActivity onCreate : ");
        if (this.getIntent().getExtras() != null) {
            Logs.fi("LTPush", " game this.getIntent().getExtras() : " + this.getIntent().getExtras().toString());
            Bundle pushBundle = this.getIntent().getExtras();
            String message_id = pushBundle.getString("google.message_id");
            Logs.fi("LTPush", " game PUSH value key : " + " google.message_id " + " value  : " + message_id);
            String message_test = pushBundle.getString("google.test");
            Logs.fi("LTPush", " game PUSH value key : " + " google.test " + " value  : " + message_test);
            if (!LTSDKUtils.isEmpty(message_id)) {
                sendStartBroadcast(this);
            }
        }


        Logs.isShowLog = true;

        mgetRegistrationID = findViewById(R.id.getRegistrationID);
        mSendLocalPush = findViewById(R.id.sendLocalPush);
        mRemoveLocalPush = findViewById(R.id.removeLocalPush);
        mRemoveAll = findViewById(R.id.removeAll);
        mCancelAllPush = findViewById(R.id.cancelAllPush);
        mcancelPush = findViewById(R.id.cancelPush);

        mgetRegistrationID.setOnClickListener(this);
        mSendLocalPush.setOnClickListener(this);
        mRemoveLocalPush.setOnClickListener(this);
        mRemoveAll.setOnClickListener(this);
        mCancelAllPush.setOnClickListener(this);
        mcancelPush.setOnClickListener(this);

        LTBaseSDK.getInstance(this).LTPushSetPushListener(mLTBaseSDKPushListener);
    }

    /**
     * 推送相关回调
     */
    private LTBaseSDKPushListener mLTBaseSDKPushListener = new LTBaseSDKPushListener() {
        @Override
        public void LTBaseSDKPushToken(int code, String token) {
            String GooglePushToken = token;
            Logs.i("LTPush", " GooglePushToken = " + GooglePushToken);
            Toast.makeText(getApplicationContext(), GooglePushToken, Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    public void onClick(View v) {

        if (v == mgetRegistrationID) {

            /**
             * 获取推送token
             */
            LTBaseSDK.getInstance(this)
                    .LTPushGetPushToken();

        } else if (v == mSendLocalPush) {
            addpushalert_edit();
        } else if (v == mRemoveLocalPush) {
            removepushalert_edit();

        } else if (v == mRemoveAll) {
            /**
             */
            LTBaseSDK.getInstance(this)
                    .LTPushClearAllNotification();
        } else if (v == mCancelAllPush) {
            LTBaseSDK.getInstance(this)
                    .LTPushRemoveAllLocalPush();
        } else if (v == mcancelPush) {
            delPushalert_edit();
        }

    }

    public void addpushalert_edit() {
        final EditText et = new EditText(this);
        et.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        new AlertDialog.Builder(this).setTitle("请输入消息id")
                .setIcon(android.R.drawable.sym_def_app_icon)
                .setView(et)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //按下确定键后的事件
//                        Toast.makeText(getApplicationContext(), et.getText().toString(),Toast.LENGTH_LONG).show();

                        Logs.fi(Logs.LTLogTag, " et.getText().toString() =  " + et.getText().toString());
                        String pushID = et.getText().toString();
                        if (LTSDKUtils.isEmpty(pushID)) {
                            Toast.makeText(getApplicationContext(), "不能输入空的", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        int noticeID = Integer.parseInt(pushID);
                        if (noticeID <= 0 || noticeID >= Integer.MAX_VALUE) {
                            Toast.makeText(getApplicationContext(), "输入不符合规范，大于0 小于" + 0x7fffffff + "的数字", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        addLocalPush(noticeID);
                    }
                }).setNegativeButton("取消", null).show();
    }


    public void removepushalert_edit() {
        final EditText et = new EditText(this);
        et.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        new AlertDialog.Builder(this).setTitle("请输入消息id")
                .setIcon(android.R.drawable.sym_def_app_icon)
                .setView(et)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //按下确定键后的事件
//                        Toast.makeText(getApplicationContext(), et.getText().toString(),Toast.LENGTH_LONG).show();

                        Logs.fi(Logs.LTLogTag, " et.getText().toString() =  " + et.getText().toString());
                        String pushID = et.getText().toString();
                        if (LTSDKUtils.isEmpty(pushID)) {
                            Toast.makeText(getApplicationContext(), "不能输入空的", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        int noticeID = Integer.parseInt(pushID);
                        if (noticeID <= 0 || noticeID >= Integer.MAX_VALUE) {
                            Toast.makeText(getApplicationContext(), "输入不符合规范，大于0 小于" + 0x7fffffff + "的数字", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        removeLocalPush(noticeID);
                    }
                }).setNegativeButton("取消", null).show();
    }

    public void delPushalert_edit() {
        final EditText et = new EditText(this);
        et.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        new AlertDialog.Builder(this).setTitle("请输入消息id")
                .setIcon(android.R.drawable.sym_def_app_icon)
                .setView(et)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //按下确定键后的事件
//                        Toast.makeText(getApplicationContext(), et.getText().toString(),Toast.LENGTH_LONG).show();

                        Logs.fi(Logs.LTLogTag, " et.getText().toString() =  " + et.getText().toString());
                        String pushID = et.getText().toString();
                        if (LTSDKUtils.isEmpty(pushID)) {
                            Toast.makeText(getApplicationContext(), "不能输入空的", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        int noticeID = Integer.parseInt(pushID);
                        if (noticeID <= 0 || noticeID >= Integer.MAX_VALUE) {
                            Toast.makeText(getApplicationContext(), "输入不符合规范，大于0 小于" + 0x7fffffff + "的数字", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        DelLocalPush(noticeID);
                    }
                }).setNegativeButton("取消", null).show();
    }


    /**
     * @param NotificationID
     */
    private void removeLocalPush(int NotificationID) {
        LTBaseSDK.getInstance(this)
                .LTPushRemoveLocalNotification(String.valueOf(NotificationID));
    }

    /**
     * @param NotificationID
     */
    private void addLocalPush(int NotificationID) {
        LTBaseSDK.getInstance(this).LTPushAddLocalNotification(AssemblyData(NotificationID));
    }

    /**
     * @param pushid
     */
    private void DelLocalPush(int pushid) {
        LTBaseSDK.getInstance(this)
                .LTPushRemoveLocalPush(String.valueOf(pushid));
    }


    /**
     * 拼装本地数据
     */
    public LTPushLocalData AssemblyData(int NotificationID) {
        LTPushLocalData mLTPushLocalData = new LTPushLocalData();

        mLTPushLocalData.setPushId(String.valueOf(NotificationID));
        mLTPushLocalData.setTitle("title" + NotificationID);
        mLTPushLocalData.setNotificationId(NotificationID);
        mLTPushLocalData.setContent("contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent");
        mLTPushLocalData.setSubtext("subtext");
        mLTPushLocalData.setTicker("ticker");
        mLTPushLocalData.setAutocancel(true);
        mLTPushLocalData.setPushChannelID("ltbase_push_google");
        mLTPushLocalData.setPushChannelName("pushChannelName");
        mLTPushLocalData.setBroadcastTime(System.currentTimeMillis() + 5000);

        return mLTPushLocalData;
    }


    /**
     * 发送游戏被拉起的广告
     */
    private void sendStartBroadcast(Context con) {
        Intent sendintent = new Intent();
        sendintent.setAction("com.longtupush.android.intent.WakedReceiver");
        sendintent.addCategory(con.getPackageName());
        con.sendBroadcast(sendintent);
    }

}
