package com.youmi.android.offer;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.IntDef;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sheep.gamegroup.absBase.AbsObserver;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import rx.functions.Action1;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by realicing on 2018/10/29.
 * realicing@sina.com
 */
public class YmConfig {
    public static final String getAppId(){
     return "35fdb087478a19f7";
    }
    public static final String getAppSecret(){
        return "7322d8d028c29a99";
    }
    public static final boolean isShowLog(){
        return TestUtil.isTest();
    }
    //是否在进入有米任务界面时提示用户总的有米积分
    public static final boolean isToastTotalPrice(){
        return false;
    }
    //通过设置TextView的方式来添加提示
    public static final int TYPE_POP = 1;
    public static final int TYPE_ADD_VIEW = 2;
    public static final int TYPE_SET_TV = 3;
    @IntDef({TYPE_POP, TYPE_ADD_VIEW, TYPE_SET_TV})
    @Retention(RetentionPolicy.SOURCE)
    public @interface SHOW_YM_TIP_TYPE {
    }
    private static PopupWindow popupWindow;
    //显示有米积分兑换率：100积分等于1绵羊币
    public static void showTipDialog(final Activity activity, @SHOW_YM_TIP_TYPE final int showYmTipType) {
        Observable.just(1).delay(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new AbsObserver<Integer>(){

                    @Override
                    public void onNext(Integer integer) {
                        View contentView = activity.findViewById(android.R.id.content);
                        try {
                            switch (showYmTipType){
                                case TYPE_ADD_VIEW:
                                    addTipViewToYmAvf(activity, contentView);
                                    break;
                                case TYPE_SET_TV:
                                    setTipViewInYmAvf(activity, contentView);
                                    break;
                                case TYPE_POP:
                                    popTipViewOnYmAvf(activity, contentView);
                                    break;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            if(BuildConfig.DEBUG) G.showToast(e.getMessage());
                        }
                    }
                });
    }
    //通过popWindow的方式显示提示：注：100积分等于1绵羊币
    private static void popTipViewOnYmAvf(Activity activity, View contentView) {
        View popView = LayoutInflater.from(activity).inflate(R.layout.ym_tip, null);
        View titleBar;
        if(contentView instanceof ViewGroup){
            titleBar = ((ViewGroup) contentView).getChildAt(0);
            if(titleBar instanceof ViewGroup){
                titleBar = ((ViewGroup) titleBar).getChildAt(0);
            }
        } else {
            titleBar = contentView;
        }
        // 创建PopupWindow对象，其中：
        // 第一个参数是用于PopupWindow中的View，第二个参数是PopupWindow的宽度，
        // 第三个参数是PopupWindow的高度，第四个参数指定PopupWindow能否获得焦点
        PopupWindow popupWindow=new PopupWindow(popView, WindowManager.LayoutParams.MATCH_PARENT, titleBar.getHeight(), false);
        // 设置PopupWindow的背景
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置PopupWindow是否能响应外部点击事件
        popupWindow.setOutsideTouchable(false);
        // 设置PopupWindow是否能响应点击事件
        popupWindow.setTouchable(false);
        // 显示PopupWindow，其中：
        // 第一个参数是PopupWindow的锚点，第二和第三个参数分别是PopupWindow相对锚点的x、y偏移
        popupWindow.showAsDropDown(titleBar, 0, -titleBar.getHeight());
        // 或者也可以调用此方法显示PopupWindow，其中：
        // 第一个参数是PopupWindow的父View，第二个参数是PopupWindow相对父View的位置，
        // 第三和第四个参数分别是PopupWindow相对父View的x、y偏移
        // window.showAtLocation(parent, gravity, x, y);
    }

    //通过修改TextView的方式显示提示：注：100积分等于1绵羊币
    private static void setTipViewInYmAvf(Activity activity, View contentView) {
        if(contentView instanceof ViewGroup){
            showChildView((ViewGroup)contentView, 0);
        }
    }

    //通过添加view的方式显示提示：注：100积分等于1绵羊币
    private static void addTipViewToYmAvf(Activity activity, View contentView) {
        if(contentView instanceof ViewGroup){
            View rootView = ((ViewGroup) contentView).getChildAt(0);
            if(rootView instanceof RelativeLayout){
                View child = LayoutInflater.from(activity).inflate(R.layout.ym_tip, null);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, ((RelativeLayout) rootView).getChildAt(0).getHeight());
                ((RelativeLayout) rootView).addView(child, layoutParams);
            }
        }
    }

    private static void showChildView(ViewGroup contentView, int cj) {
        int count = contentView.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = contentView.getChildAt(i);
            if(view instanceof ViewGroup)
                showChildView((ViewGroup) view, cj +1);
            else if(view instanceof TextView) {
                String text = ((TextView) view).getText().toString();
//                if(TextUtils.equals(text, "0")){//显示在标题栏有米积分的右边的logo的位置，有背景，字较小
                if(TextUtils.equals(text, "积分")){//显示在标题栏右边
                    view.setVisibility(View.VISIBLE);
                    ((TextView) view).setText(R.string.ym_tip);
                }
                LogUtil.println("showChildView", cj, i, contentView, view.getId(), view.getClass().getSimpleName(), text);
            } else {
                LogUtil.println("showChildView", cj, i, contentView, view.getId(), view.getClass().getSimpleName());
            }
        }
    }


    //取消弹出框
    public static void disTipDialog() {
        try {
            if(popupWindow != null){
                popupWindow.dismiss();
                popupWindow = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            if(BuildConfig.DEBUG) G.showToast(e.getMessage());
        }
    }

    public static boolean isAvf(Activity activity) {
        return activity instanceof cde.ewd.adw.AVF;
//        return activity instanceof ActMyMoney;
    }
}
