package com.prankapp.fartsounds.airhorn.ui;

import android.animation.ObjectAnimator;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.prankapp.fartsounds.airhorn.R;
import com.prankapp.fartsounds.airhorn.ad_util.AdPrankListener;
import com.prankapp.fartsounds.airhorn.model.PrankListModel;
import com.prankapp.fartsounds.airhorn.openmediation.AdManager;
import com.prankapp.fartsounds.airhorn.utilsfff.Constant;
import com.prankapp.fartsounds.airhorn.utilsfff.ImmUtils;
import com.prankapp.fartsounds.airhorn.utilsfff.SPUtils;
import com.prankapp.fartsounds.airhorn.utilsfff.ShakeUtils;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class PrankDetailsActivity extends AppCompatActivity {

    ImageView iv_back;
    ImageView ivContent;
    ImageView iv_bofang;
    TextView tv_shake_tips;

    boolean isBoFang = false;
    boolean isYaoHuangBoFang = false;
    boolean isPrepare = false;
    LoadingDialog loadingDialog;
    PrankListModel.ListBean itemContent;
    private ImageView iv_shoucang;
    ShakeUtils mShakeUtils;
    int jj = 0;
    private List<PrankListModel> pankList;
    ObjectAnimator animator;

    boolean isPlayAd = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prank_details);
        loadingDialog = new LoadingDialog(this);
        loadingDialog
                .setLoadingText("loading...")
                .setSuccessText("Success")
                .setInterceptBack(true)
                .show();
        ImmUtils.setStatusBar(this, false, false);

        iv_back = findViewById(R.id.iv_back);
        tv_shake_tips = findViewById(R.id.tv_shake_tips);
        ivContent = findViewById(R.id.iv_conent);
        iv_bofang = findViewById(R.id.iv_bofang);
        iv_shoucang = findViewById(R.id.iv_shoucang);

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdManager.showAD(PrankDetailsActivity.this, new Function1<Integer, Unit>() {
                    @Override
                    public Unit invoke(Integer integer) {
                        if (integer == AdManager.type_show_close || integer == AdManager.type_no_cache || integer == AdManager.type_show_fail) {
                            finish();
                        }
                        return null;
                    }
                });

            }
        });


        String id = getIntent().getStringExtra("id");
        ArrayList<PrankListModel.ListBean> listBeans = new ArrayList<>();

        isShoucah(id);


        for (int i = 0; i < Constant.prankListModel.size(); i++) {
            if (Constant.prankListModel.get(i).getCategoryId().equals(id)) {
                jj = i;
                listBeans.addAll(Constant.prankListModel.get(i).getList());
            }
        }

        itemContent = listBeans.get(0);

        iv_shoucang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isShoucah(id)) {
                    pankList.remove(getIndex(id));
                    Gson gson = new Gson();
                    String json = gson.toJson(pankList);
                    SPUtils.getInstance().put(Constant.shouCang, json);
                } else {
                    pankList.add(Constant.prankListModel.get(jj));
                    Gson gson = new Gson();
                    String json = gson.toJson(pankList);
                    SPUtils.getInstance().put(Constant.shouCang, json);

                }
                isShoucah(id);

            }
        });

        if (mShakeUtils == null) {

            mShakeUtils = new ShakeUtils(this);

        }


        Glide.with(this).load(itemContent.getPreUrl()).into(ivContent);

        try {
            Constant.mediaPlayer.reset();
            Constant.mediaPlayer.setDataSource(itemContent.getMp3Url());
            Constant.mediaPlayer.setLooping(true);

        } catch (IOException e) {
            e.printStackTrace();
        }
        Constant.mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                isPrepare = true;
                loadingDialog.loadSuccess();
            }
        });

        if (id.equals("625f83491fea07887e61906a") || id.equals("636b8e5415831ec42fc7d80a")) {//左右摇晃
            // 设置动画的初始位置，0表示当前位置
            animator = ObjectAnimator.ofFloat(ivContent, "translationX", -20f, 20f);//上下摇晃
        } else if (id.equals("625f83491fea07887e61906b") || id.equals("62be51cc4d4edd56b588d54e") || id.equals("62be51f84d4edd56b588d54f") || id.equals("625f83491fea07887e619065") || id.equals("64b617f06d7b94ebbe2a20fa")) {
            tv_shake_tips.setVisibility(View.VISIBLE);
            iv_bofang.setVisibility(View.INVISIBLE);
            animator = ObjectAnimator.ofFloat(ivContent, "translationY", 0f, 20f, -20f, 20f, -20f, 20f, 0f);
        } else {
            animator = ObjectAnimator.ofFloat(ivContent, "translationY", 0f, 0f);
        }

        // 动画时长
        animator.setDuration(30);

        // 重复次数，-1表示无限重复
        animator.setRepeatCount(-1);

        // 重复模式，ObjectAnimator.REVERSE表示反向
        animator.setRepeatMode(ObjectAnimator.REVERSE);

        Constant.mediaPlayer.prepareAsync();

        Constant.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                iv_bofang.setImageDrawable(getDrawable(R.mipmap.ic_bofang1));
            }
        });

        mShakeUtils.setOnShakeListener(new ShakeUtils.OnShakeListener() {
            @Override
            public void onShake() {
                if (id.equals("625f83491fea07887e61906b") || id.equals("62be51cc4d4edd56b588d54e") || id.equals("62be51f84d4edd56b588d54f") || id.equals("625f83491fea07887e619065") || id.equals("64b617f06d7b94ebbe2a20fa")) {
                    Log.e("666", "摇晃了");
                    playYaoHuangSingle();
                }
            }
        });

        iv_bofang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!isPlayAd) {
                    isPlayAd = true;
                    if (!isPrepare) {
                        return;
                    }
                    AdManager.showAD(PrankDetailsActivity.this, new Function1<Integer, Unit>() {
                        @Override
                        public Unit invoke(Integer integer) {
                            if (integer == AdManager.type_show_close || integer == AdManager.type_no_cache || integer == AdManager.type_show_fail) {
                                if (isBoFang) {
                                    isBoFang = false;
                                    Constant.mediaPlayer.pause();
                                    iv_bofang.setImageDrawable(getDrawable(R.mipmap.ic_bofang1));
                                    animator.cancel();
                                } else {
                                    Constant.mediaPlayer.start();
                                    isBoFang = true;
                                    iv_bofang.setImageDrawable(getDrawable(R.mipmap.ic_zanting1));
                                    animator.start();
                                }
                            }
                            return null;
                        }
                    });

                } else {

                    if (!isPrepare) {
                        return;
                    }

                    if (isBoFang) {
                        isBoFang = false;
                        Constant.mediaPlayer.pause();
                        iv_bofang.setImageDrawable(getDrawable(R.mipmap.ic_bofang1));
                        animator.cancel();
                    } else {
                        Constant.mediaPlayer.start();
                        isBoFang = true;
                        iv_bofang.setImageDrawable(getDrawable(R.mipmap.ic_zanting1));
                   /* if(id.equals("625f83491fea07887e61906b")){
                        animator.start();
                    }*/
                        animator.start();
                    }

                }

            }
        });
    }

    public void playYaoHuangSingle() {

        if (!isPlayAd) {
            isPlayAd = true;
            if (!isPrepare) {
                return;
            }
            AdManager.showAD(PrankDetailsActivity.this, new Function1<Integer, Unit>() {
                @Override
                public Unit invoke(Integer integer) {
                    if (integer == AdManager.type_show_close || integer == AdManager.type_no_cache || integer == AdManager.type_show_fail) {
                        if (isYaoHuangBoFang) {
                            isYaoHuangBoFang = false;
                            Constant.mediaPlayer.pause();
                            animator.cancel();
                        } else {
                            Constant.mediaPlayer.start();
                            isYaoHuangBoFang = true;
                            animator.start();
                        }
                    }

                    return null;
                }
            });


        } else {
            if (!isPrepare) {
                return;
            }
            if (isYaoHuangBoFang) {
                isYaoHuangBoFang = false;
                Constant.mediaPlayer.pause();
                animator.cancel();
            } else {
                Constant.mediaPlayer.start();
                isYaoHuangBoFang = true;
                animator.start();
            }
        }

    }

    public void playLoop() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isBoFang || isYaoHuangBoFang) {
            Constant.mediaPlayer.stop();
        }
    }

    protected void onResume() {
        super.onResume();
        if (mShakeUtils != null) {
            mShakeUtils.onResume();
        }
    }

    protected void onPause() {
        super.onPause();
        if (mShakeUtils != null) {
            mShakeUtils.onPause();
        }
    }


    public boolean isShoucah(String id) {
        iv_shoucang.setImageDrawable(getResources().getDrawable(R.mipmap.xihuan2));
        String shouCang = SPUtils.getInstance().getString(Constant.shouCang);
        pankList = new ArrayList<>();
        if (!shouCang.isEmpty()) {
            JSONArray jsonArray = JSONArray.parseArray(shouCang);
            for (int i = 0; i < jsonArray.size(); i++) {
                PrankListModel prankListModel = JSON.parseObject(jsonArray.get(i).toString(), PrankListModel.class);
                pankList.add(prankListModel);
            }
        }

        boolean sss = false;
        for (int i = 0; i < pankList.size(); i++) {
            if (Objects.equals(pankList.get(i).getCategoryId(), id)) {
                sss = true;
                iv_shoucang.setImageDrawable(getResources().getDrawable(R.mipmap.xihuan1));
            }
        }
        return sss;
    }

    public int getIndex(String id) {
        int reti = 0;
        for (int i = 0; i < pankList.size(); i++) {
            if (Objects.equals(pankList.get(i).getCategoryId(), id)) {
                reti = i;
            }
        }

        return reti;


    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        AdManager.showAD(PrankDetailsActivity.this, new Function1<Integer, Unit>() {
            @Override
            public Unit invoke(Integer integer) {
                return null;
            }
        });

    }
}
