BaseHomeActivity.java 42.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
package com.lotus.town;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ad.lib.RewardManager;
import com.ad.lib.tt.TTInterstitialActivity;
import com.lotus.town.dialog.Dialog;
import com.lotus.town.event.BaseEvent;
import com.lotus.town.home.HomeItem;
import com.lotus.town.home.HomeLevelConfig;
import com.lotus.town.home.HomeManager;
import com.lotus.town.home.ItemType;
import com.lotus.town.sound.SoundManager;
import com.lotus.town.utils.SharedPrefConfig;
import com.lotus.town.utils.Utils;
import com.lotus.town.widget.HehuaView;
import com.lotus.town.widget.PowerCollectAnim;
import com.sdk.Sdk;
import com.sdk.log.LogConstants;
import com.sdk.utils.PxUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BaseHomeActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    protected boolean isHomePage = false;
    private static final int MSG_JULI = 0;
    private static final int MSG_JUMP = 1;
    private static final int MSG_NOT_HIT = 2;
    private static final int MSG_HIT = 3;
    private static final int MSG_JUMP_FINISH = 4;

    private LinearLayout mQingWa;
    private ImageView mQingWaView;
    private int QWHeight;
    private int QWWidth;
    private int GuideWidth;
    private float src_x;
    private float src_y;
    private int jumpDistance = 0;
    private float animationTimer = 0;
    private ImageView mNewWaterFlower;
    private float mQWPositionX = 0;
    private float mQWPositionY = 0;
    float scaley = 0;
    private boolean isUp = true;
    private boolean isPressedMove = false;
    private boolean isPressedLauncher = false;
    private ImageView mHeHuaBg;
    private ImageView mGroundIv;

    private ImageView mSoundOffOn;
    private PowerCollectAnim mPowerAnim;

    private List<HehuaView> mHehuaViewList = new ArrayList<>();

    private Rect mMoveRect = null;
    private Rect mLauncherRect = null;

    private int HHHeight;
    private int HHWidth;
    private ImageView mGuideView;

    private HomeLevelConfig config = null;
    private int SCREEN_WIDTH = 0;
    private int SCREEN_HEIGHT = 0;
    private LinearLayout mHeHuaLinearLayout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResource());
        EventBus.getDefault().register(this);
        mQingWa = findViewById(R.id.qingwa);
        mQingWaView = findViewById(R.id.qingwa_view);
        mGuideView = findViewById(R.id.jump_guide);
        mHeHuaBg = findViewById(R.id.stone);
        mGroundIv = findViewById(R.id.ground);
        Display display = getWindowManager().getDefaultDisplay();
        SCREEN_WIDTH = display.getWidth();
        SCREEN_HEIGHT = display.getHeight();

        mHeHuaLinearLayout = findViewById(R.id.hehua_row_1);
        ViewGroup.LayoutParams params = mHeHuaLinearLayout.getLayoutParams();
        ViewGroup.MarginLayoutParams marginParams = null;
        //获取view的margin设置参数
        if (params instanceof ViewGroup.MarginLayoutParams) {
            marginParams = (ViewGroup.MarginLayoutParams) params;
        } else {
            marginParams = new ViewGroup.MarginLayoutParams(params);
        }
        marginParams.topMargin = SCREEN_HEIGHT/3;
        mHeHuaLinearLayout.setLayoutParams(marginParams);
        mHehuaViewList.add((HehuaView) findViewById(R.id.hehua_1));
        mHehuaViewList.add((HehuaView) findViewById(R.id.hehua_2));
        mHehuaViewList.add((HehuaView) findViewById(R.id.hehua_3));
        mHehuaViewList.add((HehuaView) findViewById(R.id.hehua_4));
        mHehuaViewList.add((HehuaView) findViewById(R.id.hehua_5));
        mHehuaViewList.add((HehuaView) findViewById(R.id.hehua_6));
        mHehuaViewList.add((HehuaView) findViewById(R.id.hehua_7));
        mHehuaViewList.add((HehuaView) findViewById(R.id.hehua_8));
        mHehuaViewList.add((HehuaView) findViewById(R.id.hehua_9));

        mNewWaterFlower = findViewById(R.id.water_flower);
        mPowerAnim = findViewById(R.id.power_collect);
        mSoundOffOn = findViewById(R.id.sound_off_on);
        mSoundOffOn.setOnClickListener(this);

        initView();

        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        mQingWa.measure(w,h);
        mHeHuaBg.measure(w,h);
        mGroundIv.measure(w,h);
        mGuideView.measure(w,h);

        for (HehuaView hehuaView : mHehuaViewList) {
            hehuaView.doMeasure();
        }

        QWHeight = mQingWa.getMeasuredHeight();
        QWWidth = mQingWa.getMeasuredWidth();
        GuideWidth = mGuideView.getMeasuredWidth();
        HHHeight = mHeHuaBg.getMeasuredHeight();
        HHWidth = mHeHuaBg.getMeasuredWidth();

        config = HomeManager.getCurrentConfig();

        initLayout();
    }

    private void refreshLayout(){
        config = HomeManager.getCurrentConfig();

        initLayout();
    }

    protected void initView() {
    }

    protected int getLayoutResource() {
        return R.layout.activity_main;
    }

    private void initLayout() {
        for(int i = 0;i<config.getItems().size();i++){
            mHehuaViewList.get(i).setItemType(config.getItems().get(i));
        }
    }

    private void initQingWa(){
      LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mQingWaView.getLayoutParams();
      layoutParams.leftMargin = 0;
      layoutParams.topMargin = 0;
      layoutParams.rightMargin = 0;
      mQingWaView.setAlpha(1f);
      mQingWaView.setLayoutParams(layoutParams);
      mQingWa.setVisibility(View.VISIBLE);
      mQingWa.setX(mQWPositionX);
      mQingWa.setY(mQWPositionY);
      scaley = 0;
      jumpStep = 0;
      gotoFirstGuide();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        launcherWelcome();
        gotoFirstGuide();
        SoundManager.getInstance().homePlayer();
    }

    private void gotoFirstGuide(){
        if(SharedPrefConfig.getInstance(this).willGuideShow()){
            if(mGuideView.getVisibility() == View.GONE) {
                mGuideView.setVisibility(View.VISIBLE);
            }
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mGuideView.getLayoutParams();
            params.bottomMargin = PxUtils.dip2px(this,136);
            mGuideView.setLayoutParams(params);
//            mGuideView.setBackgroundResource(R.drawable.guide_left);
        }
    }

    private void gotoSecondGuide(){
//        if(mGuideView.getVisibility() == View.GONE) {
//            return;
//        }
//        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mGuideView.getLayoutParams();
//        params.bottomMargin = PxUtils.dip2px(this,40);
//        mGuideView.setLayoutParams(params);
//        mGuideView.setBackgroundResource(R.drawable.guide_press);
    }

    private void hideGuide(){
        mGuideView.setVisibility(View.GONE);
    }

    private void launcherWelcome(){
        if(!uiManager.getInstance(this).isLogin() && SharedPrefConfig.getInstance(this).showLoginReminder()){
            Intent i = new Intent();
            i.setClass(mContext,WelcomeActivity.class);
            this.startActivityForResult(i,100);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 100){
            gotoFirstGuide();
        }
    }

    private void jumpAnition(float x){
        float tartget_y = mQingWa.getY()-jumpDistance;
        mQingWa.setY(tartget_y);
    }

    private void initWidgetPosition(){
        if(mQWPositionX == 0){
            mQWPositionX = mQingWa.getX();
        }
        if(mQWPositionY == 0){
            mQWPositionY = mQingWa.getY();
            mPowerAnim.initPosition(mQWPositionX+QWWidth/2,mQWPositionY+QWHeight/2);
        }
        if(mMoveRect == null){
            mMoveRect = new Rect((int)mHeHuaBg.getX(),(int)mHeHuaBg.getY(),(int)mHeHuaBg.getX()+HHWidth,(int)mHeHuaBg.getY()+HHHeight);
        }

        if(mLauncherRect == null){
            mLauncherRect = new Rect(0, (int)mGroundIv.getY(),SCREEN_WIDTH,SCREEN_HEIGHT);
        }

        for (HehuaView hehuaView : mHehuaViewList) {
            hehuaView.getValidRect(QWWidth, QWHeight);
        }
    }

    protected boolean hasHehua() {
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        initWidgetPosition();

        if (event.getAction()== MotionEvent.ACTION_DOWN){
            if(mMoveRect.contains((int)event.getX(),(int)event.getY())){
                isUp = false;
                isPressedMove = true;
            } else if(mLauncherRect.contains((int)event.getX(),(int)event.getY())){

                if(isQWdead()){
                    showJumpResult(ItemType.BOMB_ITEM, null, 0);
                    return false;
                }

                if(hasHehua()){
                    showJumpResult(ItemType.TIMES_OUT,null, 0);
                    return false;
                }
                isUp = false;
                isPressedLauncher = true;

                if (!mHandler.hasMessages(MSG_JULI)) {
                    SoundManager.getInstance().playerPowerCollect();
                    mPowerAnim.setVisibility(View.VISIBLE);
                    mHandler.sendEmptyMessageDelayed(MSG_JULI, 10);
                }
            }
        } else if (event.getAction()== MotionEvent.ACTION_UP){
            isUp = true;
            if(isPressedMove){
                isPressedMove = false;
                mMoveRect = new Rect((int)mHeHuaBg.getX(),(int)mHeHuaBg.getY(),(int)mHeHuaBg.getX()+HHWidth,(int)mHeHuaBg.getY()+HHHeight);
                mQWPositionX = mQingWa.getX();
                mPowerAnim.initPosition(mQWPositionX+QWWidth/2,mQWPositionY+QWHeight/2);
                gotoSecondGuide();
            } else if(isPressedLauncher){
                SharedPrefConfig.getInstance(this).guideShow();
                SoundManager.getInstance().stopPowerCollect();
                mPowerAnim.setVisibility(View.GONE);
                mPowerAnim.reset();
                isPressedLauncher = false;
                if(!mHandler.hasMessages(MSG_JUMP)) {
                    doJump();
                    mHandler.sendEmptyMessageDelayed(MSG_JUMP, 10);
                }
                hideGuide();
            }

        } else if(event.getAction() == MotionEvent.ACTION_MOVE){
            if(isPressedMove){
                int dx = (int) event.getRawX() - HHWidth/2;
                int dx2 = (int) event.getRawX() - QWWidth/2;
                int dx3 = (int) event.getRawX() - GuideWidth/2;
                mHeHuaBg.setX(dx);
                mQingWa.setX(dx2);
                mGuideView.setX(dx3);
            }
        }
        return super.onTouchEvent(event);
    }

    protected void doJump() {

    }

    private int jumpStep = 0;

    @Override
    protected void HandleMessage(Message msg) {
        if (msg.what == MSG_JULI) {

            scaleImage(mQingWa);
            mPowerAnim.invalidate();
            if (!isUp) {
                mHandler.sendEmptyMessageDelayed(MSG_JULI, 50);
            }
        } else if (msg.what == MSG_JUMP) {
            mQingWa.setScaleY(1.0f);
//            //调用贝塞尔曲线

//            mQingWa.setPivotY(QWHeight / 2);
//            mQingWa.setPivotX(QWWidth / 2);
            src_x = mQingWa.getX();
            src_y = mQingWa.getY();
            jumpStep++;
            if(jumpStep == 3){
                mQingWaView.setBackgroundResource(R.drawable.qwjump);
            } else if(jumpStep == 8){
                mQingWaView.setBackgroundResource(R.drawable.qingwa);
            }
            jumpAnition(animationTimer);
            animationTimer = animationTimer + 0.1f;
            if (animationTimer > 1.0f) {
                Result result = successOnly();
                if(result.index == 0){
                    mHandler.sendEmptyMessage(MSG_JUMP_FINISH);
                } else {
                    mHandler.sendEmptyMessageDelayed(MSG_JUMP_FINISH, 1000);
                }
            } else {
                mHandler.sendEmptyMessageDelayed(MSG_JUMP, 100);
            }
        } else if (msg.what == MSG_NOT_HIT) {
            jumpIntoWater();
        } else if(msg.what == MSG_HIT){
            initQingWa();
        } else if(msg.what == MSG_JUMP_FINISH){
            jumpDistance = 0;
            animationTimer = 0;
//                mQingWa.setPivotY(QWHeight);
//                mQingWa.setPivotX(QWWidth / 2);
            Result result = success();
            if (result.index == 0) {
                Sdk.logger().logEvent(this, LogConstants.LOG_JUMP_WATER,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);

                insertNotes("没有跳中荷花");
                if (result.position == HehuaView.HitPosition.NULL) {
                    mHandler.sendEmptyMessageDelayed(MSG_NOT_HIT, 100);
                    mNewWaterFlower.setX(mQingWa.getX() + QWWidth / 2 - getResources().getDimensionPixelSize(R.dimen.width_water_flower) / 2);
                    mNewWaterFlower.setY(mQingWa.getY() + QWWidth - getResources().getDimensionPixelSize(R.dimen.height_water_flower));
                    mNewWaterFlower.setVisibility(View.VISIBLE);
                } else if (result.position == HehuaView.HitPosition.NULL.LEFT) {
                    slideIntoWater(result.position);
                } else if (result.position == HehuaView.HitPosition.RIGHT) {
                    slideIntoWater(result.position);
                }
            } else {
                mHandler.sendEmptyMessageDelayed(MSG_HIT, 100);
//                    Toast.makeText(mContext,"成功:"+successId,Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        SoundManager.getInstance().homeStop();
    }

    private void slideIntoWater(final HehuaView.HitPosition position) {
        Animator animator;
        if (position == HehuaView.HitPosition.LEFT) {
            animator = ValueAnimator.ofFloat(0, -1);
        } else {
            animator = ValueAnimator.ofFloat(0, 1);
        }
        animator.setDuration(500);
        ((ValueAnimator) animator).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float margin = (float) animation.getAnimatedValue();
                Log.d(TAG, "onAnimationUpdate: margin:" + margin);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mQingWaView.getLayoutParams();
                layoutParams.leftMargin = (int) (margin * QWWidth);
                mQingWaView.setLayoutParams(layoutParams);
                mQingWaView.setAlpha(1f - Math.abs(margin));
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                initQingWa();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.start();
    }

    private void jumpIntoWater() {
        final AnimationDrawable animationDrawable = getWaterFlower();
        Animator animator = ValueAnimator.ofInt(-10, -QWHeight);
        animator.setDuration(200);
        ((ValueAnimator) animator).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int margin = (int) animation.getAnimatedValue();
                Log.d(TAG, "onAnimationUpdate: margin:" + margin);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mQingWaView.getLayoutParams();
                layoutParams.topMargin = margin;
                mQingWaView.setLayoutParams(layoutParams);

        }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                animationDrawable.start();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                notifyNotHit();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.start();
    }

    private AnimationDrawable getWaterFlower() {
        int duration;
        mNewWaterFlower.setImageResource(R.drawable.water_flower);
        AnimationDrawable animationDrawable = (AnimationDrawable) mNewWaterFlower.getDrawable();
        duration = animationDrawable.getDuration(0) * animationDrawable.getNumberOfFrames();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initQingWa();
                mNewWaterFlower.setVisibility(View.GONE);

            }
        }, duration);
        return animationDrawable;
    }


    private void scaleImage(View im){
        if(scaley >=0.5){
            return;
        }
//        if(isReverse){
//            jumpDistance = jumpDistance - 3;
//            im.setScaleY(1.0f - scaley);
//            scaley = scaley - 0.01f;
//            if(scaley <=0){
//                isReverse = false;
//            }
//        } else {
            jumpDistance = jumpDistance + 3;
            im.setScaleY(1.0f - scaley);
            scaley = scaley + 0.01f;
//            if(scaley >=0.5){
//                isReverse = true;
//            }
//        }
    }

    @Override
    public void onClick(View view) {
        if(mSoundOffOn == view){
            if(SharedPrefConfig.getInstance(this).isSoundEnable()){
                SoundManager.getInstance().stopAll();
                mSoundOffOn.setBackgroundResource(R.drawable.off);
            } else {
                SoundManager.getInstance().start();
                mSoundOffOn.setBackgroundResource(R.drawable.laba);
            }
        }
    }

    private class Result {
        int index;
        HehuaView.HitPosition position;

        public Result(int index, HehuaView.HitPosition position) {
            this.index = index;
            this.position = position;
        }
    }


    public Result successOnly() {
        int index = 0;
        //青蛙屁股和中心点落在荷叶中就算
        int x = (int) (mQingWa.getX() + QWWidth/2);
        int y = (int) (mQingWa.getY() + QWHeight);
        int x2 = (int) (mQingWa.getX() + QWWidth/2);
        int y2 = (int) (mQingWa.getY() + QWHeight/2);
        boolean hit = false;
        int hitIndex = 0;
        for (HehuaView hehuaView : mHehuaViewList) {
            if (hehuaView.getValidRect().contains(x, y) || hehuaView.getValidRect().contains(x2, y2)) {
                hit = true;
                break;
            }
            hitIndex++;
        }


        HehuaView.HitPosition position = HehuaView.HitPosition.NULL;
        if (hit) {
            index = hitIndex + 1;
        } else {
            position = getPosition();
        }

        return new Result(index, position);
    }

    public Result success() {
        int index = 0;
        //青蛙屁股和中心点落在荷叶中就算
        int x = (int) (mQingWa.getX() + QWWidth/2);
        int y = (int) (mQingWa.getY() + QWHeight);
        int x2 = (int) (mQingWa.getX() + QWWidth/2);
        int y2 = (int) (mQingWa.getY() + QWHeight/2);
        boolean hit = false;
        int hitIndex = 0;
        HehuaView hitHehuaView = null;
        for (HehuaView hehuaView : mHehuaViewList) {
            if (hehuaView.getValidRect().contains(x, y) || hehuaView.getValidRect().contains(x2, y2)) {
                hitHehuaView = hehuaView;
                hit = true;
                break;
            }
            hitIndex++;
        }


        HehuaView.HitPosition position = HehuaView.HitPosition.NULL;
        if (hit) {
            index = hitIndex + 1;
            HomeItem homeItem = config.getItems().get(hitIndex);
            jumpResult(hitHehuaView, homeItem.getType(), homeItem.getMoney());
        } else {
            position = getPosition();
        }

        return new Result(index, position);
    }



    private HehuaView.HitPosition getPosition() {
        HehuaView.HitPosition position;
        for (HehuaView hehuaView : mHehuaViewList) {
            if ((position = hehuaView.getHitPosition(new Rect((int) mQingWa.getX(), (int) mQingWa.getY(), (int) (mQingWa.getX() + QWWidth), (int) (mQingWa.getY() + QWHeight)))) != HehuaView.HitPosition.NULL) {
                return position;
            }
        }
        return HehuaView.HitPosition.NULL;
    }

    protected boolean isQWdead() {
        return false;
    }

    protected void notifyBoom() {

    }

    protected void notifyNotHit() {
        String hint = "很遗憾,就差一点点";
        Random random = new Random(System.currentTimeMillis());
        Dialog dialog = getDialog();
        dialog.dismiss(this);
        dialog.showCenter(this,
                hint,
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismissDialog();
                    }
                },
                "继续参赛",
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismissDialog();
                    }
                });
    }

    public ItemType jumpResult(HehuaView hehuaView, int type,double money){
        ItemType result = ItemType.LUCK_NEXT;
        if(type == 0){
            result = ItemType.BOMB_ITEM;
            Sdk.logger().logEvent(this, LogConstants.LOG_JUMP_BOMP,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);

            SoundManager.getInstance().jumpBomp();
            notifyBoom();
        } else if(type == 1){
            Sdk.logger().logEvent(this, LogConstants.LOG_JUMP_LUCKY,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);

            result = ItemType.LUCK_NEXT;
            SoundManager.getInstance().jumpLucky();
        } else if(type == 2){
            Sdk.logger().logEvent(this, LogConstants.LOG_JUMP_MONEY,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);
            result = ItemType.EARN_MONEY;
            SoundManager.getInstance().jumpMoney();
        } else if (type == 3) {
            result = ItemType.POWER;
            // TODO: 2019/6/30 sound and log
        }

        showJumpResult(result, hehuaView, money);

        return result;
    }

    protected void showJumpResult(ItemType itemType, View view, final double money) {
        final Dialog dialog = getDialog();
        switch (itemType) {
            case BOMB_ITEM:
                if (view != null) {
                    final ImageView bombIv = view.findViewById(R.id.hehua_top);

                    final Animator animator = ValueAnimator.ofFloat(0, 1);
                    animator.setDuration(500);
                    ((ValueAnimator) animator).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            float scale = (float) ((ValueAnimator) animator).getAnimatedValue();
                            bombIv.setScaleX(scale);
                            bombIv.setScaleY(scale);
                        }
                    });
                    animator.addListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {
                            bombIv.setBackgroundResource(R.drawable.baozha);
                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            bombIv.setBackgroundResource(R.drawable.pong);
                            showBombDialog(dialog);
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {

                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {

                        }
                    });
                    animator.start();
                } else {
                  showBombDialog(dialog);
                }
                break;

            case LUCK_NEXT:
                final int hehuaNum = 1+ new Random().nextInt(3);
                insertNotes("得到好运福,获取领取"+hehuaNum+"朵荷花机会");
                dialog.show(this,getString(R.string.frog_healing_main_hint), getString(R.string.luck_word,hehuaNum), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        insertNotes("获得了"+hehuaNum+"朵荷花,但是没有领取");
                        dismissDialog();
                        refreshLayout();
                        startVideo();
                    }
                }, getString(R.string.home_hit_type_hehua_left_btn_hint),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Sdk.logger().logEvent(BaseHomeActivity.this, LogConstants.LOG_JUMP_HEHUA_IGNORE,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);
                                insertNotes("获得了"+hehuaNum+"朵荷花,但是没有领取");
                                dismissDialog();
                                refreshLayout();
                                startVideo();
                            }
                        },getString(R.string.home_hit_type_money_right_btn_hint), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dismissDialog();
                        refreshLayout();
                        Sdk.logger().logEvent(BaseHomeActivity.this, LogConstants.LOG_JUMP_HEHUA_GET_HEHUA,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);

                        RewardManager.reward(BaseHomeActivity.this, new RewardManager.RewardListener() {
                            @Override
                            public void onRewardVerify() {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        scManager.getInstance(BaseHomeActivity.this).addLotus(hehuaNum);
                                        showAwardHehuaDialog(hehuaNum);
                                    }
                                });
                            }

                            @Override
                            public void onVideoClose() {
//                                            dismissDialog();
                            }
                        });
                    }
                });
                break;
            case EARN_MONEY:
                dialog.show(this,
                        getString(R.string.home_hit_type_money_main_hint),
                        getString(R.string.home_hit_type_money_sub_hint, money + ""),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Sdk.logger().logEvent(BaseHomeActivity.this, LogConstants.LOG_JUMP_MONEY_IGNORE,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);
                                insertNotes("赚到了"+Utils.doubleToString(money)+"元,但是没有领取");
                                dismissDialog();
                                refreshLayout();
                                startVideo();
                            }
                        },
                        getString(R.string.home_hit_type_money_left_btn_hint),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Sdk.logger().logEvent(BaseHomeActivity.this, LogConstants.LOG_JUMP_MONEY_IGNORE,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);
                                insertNotes("赚到了"+Utils.doubleToString(money)+"元,但是没有领取");
                                dismissDialog();
                                refreshLayout();
                                startVideo();
                            }
                        },
                        getString(R.string.home_hit_type_money_right_btn_hint),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                {
                                    Sdk.logger().logEvent(BaseHomeActivity.this, LogConstants.LOG_JUMP_MONEY_GET_MONEY,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);

                                    RewardManager.reward(BaseHomeActivity.this, new RewardManager.RewardListener() {
                                        @Override
                                        public void onRewardVerify() {
                                            Log.d(TAG, "onRewardVerify() called");
                                            final double currentMoney = isHomePage?money:money/2;
//                                            UIUtils.showToast("已经赚取"+currentMoney+"元");
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    showAwardDialog(currentMoney);
                                                }
                                            });
                                            insertNotes("赚到了"+currentMoney+"元");
                                            scManager.getInstance(BaseHomeActivity.this).earnMoney(currentMoney);
                                            notifyNeighbourMoneyEarn(currentMoney);
                                        }

                                        @Override
                                        public void onVideoClose() {
//                                            dismissDialog();
                                        }
                                    });
                                }
                            }
                        }
                );
                break;
            case TIMES_OUT:
                dialog.show(this,getString(R.string.home_lotus_ran_out_main_hint), getString(R.string.home_lotus_ran_out_sub_hint, Utils.doubleToString(scManager.getInstance(this).getTodayMoney())), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dismissDialog();
                    }
                }, null,null,getString(R.string.home_lotus_ran_out_right_btn_hint), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dismissDialog();
                        Intent i = new Intent();
                        i.setClass(BaseHomeActivity.this,EarnHehuaActivity.class);
                        BaseHomeActivity.this.startActivity(i);
                    }
                });
                break;
            case ALIVE_QW:
                dialog.show(this,getString(R.string.frog_healing_main_hint), getString(R.string.frog_healing_sub_hint), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dismissDialog();
                        refreshLayout();
                    }
                }, null,null,getString(R.string.frog_healing_right_btn_hint), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dismissDialog();
                        refreshLayout();
                    }
                });
                break;
            case POWER:
                dialog.show(this,
                        getString(R.string.dlg_get_power_main_hint),
                        getString(R.string.dlg_get_power_sub_hint, Utils.doubleToInt(money)),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startVideo();
                                dismissDialog();
                            }
                        },
                        getString(R.string.dlg_get_power_left_btn_hint),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startVideo();
                                dismissDialog();
                            }
                        },
                        getString(R.string.dlg_get_power_right_btn_hint),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                {
                                    RewardManager.reward(BaseHomeActivity.this, new RewardManager.RewardListener() {
                                        @Override
                                        public void onRewardVerify() {
                                            Log.d(TAG, "onRewardVerify() called");
                                            final int power = Utils.doubleToInt(money);
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    int currentLevel = scManager.getInstance(getApplicationContext()).getQingwaCurrentLevel();
                                                    int distancePower = scManager.getInstance(getApplicationContext()).addPower(power);
                                                    int newLevel = scManager.getInstance(getApplicationContext()).getQingwaCurrentLevel();
                                                    showWinPowerDialog(power, currentLevel, newLevel, distancePower);
                                                }
                                            });
                                            insertNotes("领取了" + power + "克能量");
                                        }

                                        @Override
                                        public void onVideoClose() {
                                        }
                                    });
                                }
                            }
                        }
                );
                break;
        }


      // TODO: 2019/6/20 记录历史
    }

    protected void notifyNeighbourMoneyEarn(double money) {

    }

    protected void insertNotes(String result) {
    }

    private void showBombDialog(Dialog dialog) {
      dialog.show(this,
              getString(R.string.home_hit_type_boom_main_hint),
              getString(R.string.home_hit_type_boom_sub_hint),
              new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  insertNotes("炸死了你的青蛙,没有复活就离开了");
                    Sdk.logger().logEvent(BaseHomeActivity.this, LogConstants.LOG_JUMP_BOMP_IGNORE,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);
                    dismissDialog();
                  startVideo();
                }
              },
              getString(R.string.home_hit_type_boom_left_btn_hint),
              new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Sdk.logger().logEvent(BaseHomeActivity.this, LogConstants.LOG_JUMP_BOMP_IGNORE,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);
                    insertNotes("炸死了你的青蛙,没有复活就离开了");
                  dismissDialog();
                  startVideo();
                }
              },
              getString(R.string.home_hit_type_boom_right_btn_hint),
              new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Sdk.logger().logEvent(BaseHomeActivity.this, LogConstants.LOG_JUMP_BOMP_ALIVE,isHomePage ?LogConstants.LOG_JUMP_FROM_SELF:LogConstants.LOG_JUMP_FROM_NEIGHB);

                    RewardManager.reward(BaseHomeActivity.this, new RewardManager.RewardListener() {
                    @Override
                    public void onRewardVerify() {
                      notifyQwHeal();
                      insertNotes("炸死了你的青蛙,你复活了你的青蛙。");
                    }

                    @Override
                    public void onVideoClose() {
                      dismissDialog();
                      if(!isQWdead()){
                        showJumpResult(ItemType.ALIVE_QW, null, 0);
                      }
                    }
                  });
                }
              }
      );
    }

    protected void notifyQwHeal() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateUI(BaseEvent event){

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void showAwardDialog(double money) {
        Log.d(TAG, "showAwardDialog() called with: money = [" + money + "]");
        final Dialog dialog = getDialog();
        dialog.dismiss(this);
        dialog.show(this,
                getString(R.string.earn_money_main_hint),
                getString(R.string.earn_money_sub_hint, Utils.doubleToString(money)),
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        refreshLayout();
                        dialog.dismiss(BaseHomeActivity.this);
                    }
                },
                getString(R.string.earn_money_right_btn_hint),
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        refreshLayout();
                        dialog.dismiss(BaseHomeActivity.this);
                    }
                });

    }

    private void showAwardHehuaDialog(int number) {
        Log.d(TAG, "showAwardHehuaDialog() called with: number = [" + number + "]");
        final Dialog dialog = getDialog();
        dialog.dismiss(this);
        dialog.show(this,
                getString(R.string.earn_hehua_main_hint),
                getString(R.string.earn_hehua_sub_hint, number),
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss(BaseHomeActivity.this);
                    }
                },
                getString(R.string.earn_hehua_right_btn_hint),
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss(BaseHomeActivity.this);
                    }
                });

    }

    private void showWinPowerDialog(int power, int currentLevel, int newLevel, int distancePower) {
        Log.d(TAG, "showGetPowerDialog() called with: power = [" + power + "]");
        String subHint;
        if (newLevel > currentLevel) {
            subHint = getString(R.string. dlg_win_power_sub_hint_upgrade,
                    newLevel,
                    Utils.doubleToString(scManager.getInstance(getApplicationContext()).getOfflineRewardSpeed()));
        } else {
            subHint = getString(R.string.dlg_win_power_sub_hint, power, distancePower);
        }

        final Dialog dialog = getDialog();
        dialog.dismiss(this);
        dialog.show(this,
                getString(R.string.dlg_win_power_main_hint),
                subHint,
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        refreshLayout();
                        dialog.dismiss(BaseHomeActivity.this);
                    }
                },
                getString(R.string.dlg_win_power_right_btn_hint),
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        refreshLayout();
                        dialog.dismiss(BaseHomeActivity.this);
                    }
                });

    }

    private void startVideo(){

        RewardManager.video(this,new RewardManager.RewardListener(){
            @Override
            public void onVideoClose() {

            }

            @Override
            public void onClicked() {

            }

            @Override
            public void onVideoPlayFinish() {

            }

            @Override
            public void onRewardVerify() {

            }

            @Override
            public void onVideoError() {
                startInterstitialInner();
            }
        });
    }

    private void startInterstitialInner(){
        Intent i = new Intent();
        i.setClass(this, TTInterstitialActivity.class);
        this.startActivity(i);
    }
}