package com.sheep.jiuyan.samllsheep.utils;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.DrawableRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.activity.ActCommentGameApp;
import com.sheep.gamegroup.view.adapter.TitleFragmentListAdapter;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;


/**
 * @ Created by Dlg
 * @ <p>TiTle:  TitleBarUtils</p>
 * @ <p>Description:通用标题栏处理工具</p>
 * @ date:  2017/11/10 10:30
 * @ QQ:    315096953
 */

public class TitleBarUtils {


    private static TitleBarUtils mTitleBarUtils;

    public synchronized static TitleBarUtils getInstance() {
        if (mTitleBarUtils == null) {
            mTitleBarUtils = new TitleBarUtils();
        }
        return mTitleBarUtils;
    }


    /**
     * 设置标题
     *
     * @param activity
     * @param title
     * @return
     */
    public TitleBarUtils setTitle(Activity activity, String title) {
        TextView titleView = activity.getWindow().getDecorView().findViewById(R.id.txt_baseactivity_title);
        titleView.setVisibility(View.VISIBLE);
        titleView.setText(title == null ? "" : title);
        return mTitleBarUtils;
    }

    /**
     * 设置标题
     *
     * @param view
     * @param title
     * @return
     */
    public TitleBarUtils setTitle(View view, String title) {
        TextView titleView = view.findViewById(R.id.txt_baseactivity_title);
        titleView.setVisibility(View.VISIBLE);
        titleView.setText(title == null ? "" : title);
        return mTitleBarUtils;
    }

    /**
     * 设置Title文本的颜色
     *
     * @param activity
     * @param color    颜色值
     * @return
     */
    public TitleBarUtils setTitleColor(Activity activity, int color) {
        TextView titleView = activity.getWindow().getDecorView().findViewById(R.id.txt_baseactivity_title);
        titleView.setTextColor(color);
        return mTitleBarUtils;
    }

    /**
     * 设置Title文本的颜色
     *
     * @param view
     * @param color 颜色值
     * @return
     */
    public TitleBarUtils setTitleColor(View view, int color) {
        TextView titleView = view.findViewById(R.id.txt_baseactivity_title);
        titleView.setTextColor(color);
        return mTitleBarUtils;
    }


    /**
     * 设置点击Title时结束activity
     *
     * @param activity
     * @return
     */
    public TitleBarUtils setTitleFinish(final Activity activity) {
        activity.getWindow().getDecorView().findViewById(R.id.img_baseactivity_title)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        activity.finish();
                    }
                });
        return mTitleBarUtils;
    }

    public TitleBarUtils setTitleBack(final Activity activity) {
        activity.getWindow().getDecorView().findViewById(R.id.img_baseactivity_title)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        activity.onBackPressed();
                    }
                });
        return mTitleBarUtils;
    }

    public TitleBarUtils setTitleFinish(final View view, final Activity activity) {
        view.findViewById(R.id.img_baseactivity_title)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        activity.finish();
                    }
                });
        return mTitleBarUtils;
    }

    public TitleBarUtils setTitleFinish(final View view, View.OnClickListener listener) {
        view.findViewById(R.id.img_baseactivity_title)
                .setOnClickListener(listener);
        return mTitleBarUtils;
    }

    public TitleBarUtils setTitleFinish(final Activity activity, View.OnClickListener listener) {
        activity.getWindow().getDecorView().findViewById(R.id.img_baseactivity_title)
                .setOnClickListener(listener);
        return mTitleBarUtils;
    }

    public TitleBarUtils setTitleFinish(final Activity activity, View view) {
        view.findViewById(R.id.img_baseactivity_title)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        activity.finish();
                    }
                });
        return mTitleBarUtils;
    }

    public static final int GONE = -2;
    public static final int INVISIBLE = 0;

    /**
     * 设置最左边的Title监听
     *
     * @param activity
     * @param drawableID      如果为0显示透明，如果写-1表示资源不变。-2为Gone，其它为显示该值资源
     * @param onClickListener 如果为null,不改监听
     */
    public TitleBarUtils setTitleListen(Activity activity, int drawableID, View.OnClickListener onClickListener) {
        ImageView imageView = activity.getWindow().getDecorView().findViewById(R.id.img_baseactivity_title);
        switch (drawableID) {
            case INVISIBLE:
                imageView.setVisibility(View.INVISIBLE);
                break;
            case GONE:
                imageView.setVisibility(View.GONE);
                return this;
            default:
                if (drawableID != -1) {
                    imageView.setImageResource(drawableID);
                }
                break;
        }

        if (onClickListener != null) {
            imageView.setOnClickListener(onClickListener);
        }
        return mTitleBarUtils;
    }

    /**
     * 设置最左边的Title监听
     *
     * @param view
     * @param drawableID      如果为0显示透明，如果写-1表示资源不变。-2为Gone，其它为显示该值资源
     * @param onClickListener 如果为null,不改监听
     */
    public TitleBarUtils setTitleListen(View view, int drawableID, View.OnClickListener onClickListener) {
        ImageView imageView = view.findViewById(R.id.img_baseactivity_title);
        if (drawableID == 0) {
            imageView.setVisibility(View.INVISIBLE);
        } else if (drawableID == -2) {
            imageView.setVisibility(View.GONE);
            return this;
        } else if (drawableID != -1) {
            imageView.setImageResource(drawableID);
        }

        if (onClickListener != null) {
            imageView.setOnClickListener(onClickListener);
        }
        return mTitleBarUtils;
    }

    public TitleBarUtils setSearchBox(Activity activity, String hint
            , TextView.OnEditorActionListener editorActionListener
            , TextChangedListener textChangedListener) {
        AppCompatAutoCompleteTextView box = activity.getWindow().getDecorView().findViewById(R.id.box_baseactivity_search);
        box.setHint(hint);
        box.setVisibility(View.VISIBLE);
        activity.getWindow().getDecorView().findViewById(R.id.txt_baseactivity_title).setVisibility(View.GONE);
        box.setOnEditorActionListener(editorActionListener);
        box.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                textChangedListener.onTextChanged(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        return this;
    }

    public void setSearchText(Activity activity, String text) {
        AppCompatAutoCompleteTextView box = activity.getWindow().getDecorView().findViewById(R.id.box_baseactivity_search);
        if (box != null) {
            box.setText(text);
            box.setSelection(text.length());
        }
    }

    public String getSearchText(Activity activity) {
        AppCompatAutoCompleteTextView box = activity.getWindow().getDecorView().findViewById(R.id.box_baseactivity_search);
        if (box != null) {
            return box.getText().toString();
        }
        return "";
    }

    /**
     * 设置最右边的普通按钮状态
     *
     * @param activity
     * @param btnText              设置文本
     * @param backgroundColorResId 设置背景,0为透明，-1不变，-2为Gone,其它为ResID
     * @param onClickListener      点击时的操作。
     */
    public TitleBarUtils setRightBtn(Activity activity, String btnText, int backgroundColorResId,
                                     View.OnClickListener onClickListener) {
        Button button = activity.getWindow().getDecorView().findViewById(R.id.btn_baseactivity_right);
        ImageButton imageButton = activity.getWindow().getDecorView().findViewById(R.id.ibtn_baseactivity_right);
        button.setVisibility(View.VISIBLE);
        imageButton.setVisibility(View.GONE);
        if (backgroundColorResId == 0) {
            button.setBackgroundColor(0x00000000);
        } else if (backgroundColorResId == -2) {
            button.setVisibility(View.GONE);
            return this;
        } else if (backgroundColorResId != -1) {
            button.setBackgroundColor(backgroundColorResId);
        }

        if (btnText != null) {
            button.setText(btnText);
        }
        //执行回调
        if (onClickListener != null) {
            button.setOnClickListener(onClickListener);
        }
        return this;
    }

    /**
     * 设置最右边的普通按钮状态
     *
     * @param view
     * @param btnText              设置文本
     * @param backgroundColorResId 设置背景,0为透明，-1不变，-2为Gone,其它为ResID
     * @param onClickListener      点击时的操作。
     */
    public TitleBarUtils setRightBtn(View view, String btnText, int backgroundColorResId,
                                     View.OnClickListener onClickListener) {
        Button button = view.findViewById(R.id.btn_baseactivity_right);
        ImageButton imageButton = view.findViewById(R.id.ibtn_baseactivity_right);
        button.setVisibility(View.VISIBLE);
        imageButton.setVisibility(View.GONE);
        if (backgroundColorResId == 0) {
            button.setBackgroundColor(0x00000000);
        } else if (backgroundColorResId == -2) {
            button.setVisibility(View.GONE);
            return this;
        } else if (backgroundColorResId != -1) {
            button.setBackgroundColor(backgroundColorResId);
        }

        if (btnText != null) {
            button.setText(btnText);
        }
        //执行回调
        if (onClickListener != null) {
            button.setOnClickListener(onClickListener);
        }
        return this;
    }


    /**
     * 设置最右边的普通按钮状态
     *
     * @param activity
     * @param backgroundImgResId 设置背景的ResID,为零透明，-1不变,-2为Gone，其它是资源
     * @param onClickListener    点击时的操作。
     */
    public TitleBarUtils setRightImgBotton(Activity activity, int backgroundImgResId, View.OnClickListener onClickListener) {
        Button button = activity.getWindow().getDecorView().findViewById(R.id.btn_baseactivity_right);
        ImageButton imageButton = activity.getWindow().getDecorView().findViewById(R.id.ibtn_baseactivity_right);
        if (button != null)
            button.setVisibility(View.GONE);
        imageButton.setVisibility(View.VISIBLE);
        if (backgroundImgResId == 0) {
            imageButton.setImageDrawable(new ColorDrawable(0x00000000));
        } else if (backgroundImgResId == -2) {
            imageButton.setVisibility(View.GONE);
            return this;
        } else if (backgroundImgResId != -1) {
            imageButton.setImageResource(backgroundImgResId);
        }
        if (onClickListener != null) {
            imageButton.setOnClickListener(onClickListener);
        }
        return this;
    }

    /**
     * 设置最右边的普通按钮状态
     *
     * @param view
     * @param backgroundImgResId 设置背景的ResID,为零透明，-1不变,-2为Gone，其它是资源
     * @param onClickListener    点击时的操作。
     */
    public TitleBarUtils setRightImgBotton(View view, int backgroundImgResId, View.OnClickListener onClickListener) {
        Button button = view.findViewById(R.id.btn_baseactivity_right);
        ImageButton imageButton = view.findViewById(R.id.ibtn_baseactivity_right);
        if (button != null)
            button.setVisibility(View.GONE);
        imageButton.setVisibility(View.VISIBLE);
        if (backgroundImgResId == 0) {
            imageButton.setImageDrawable(new ColorDrawable(0x00000000));
        } else if (backgroundImgResId == -2) {
            imageButton.setVisibility(View.GONE);
            return this;
        } else if (backgroundImgResId != -1) {
            imageButton.setImageResource(backgroundImgResId);
        }
        if (onClickListener != null) {
            imageButton.setOnClickListener(onClickListener);
        }
        return this;
    }

    /**
     * 设置红点是否显示
     *
     * @param activity
     * @param visibility
     */
    public void setRightRedPoint(Activity activity, int visibility) {

        activity.getWindow().getDecorView().findViewById(R.id.img_download_manager_dots).setVisibility(visibility);
    }

    /**
     * 设置右边按钮显示状态
     *
     * @param activity
     * @param visibility
     */
    public void setRightBtnVisibility(Activity activity, int visibility) {
    }

    /**
     * 设置右边图片按钮显示状态
     *
     * @param activity
     * @param visibility
     */
    public void setRightImgBtnVisibility(Activity activity, int visibility) {
        activity.getWindow().getDecorView().findViewById(R.id.ibtn_baseactivity_right).setVisibility(visibility);
    }

    /**
     * 获取右边按钮实例
     *
     * @param activity
     * @return
     */
    public Button getRightBtn(Activity activity) {
        return (Button) activity.getWindow().getDecorView().findViewById(R.id.btn_baseactivity_right);
    }

    /**
     * 获取右边图片按钮实例
     *
     * @param activity
     * @return
     */
    public ImageButton getRightImgBtn(Activity activity) {
        return (ImageButton) activity.getWindow().getDecorView().findViewById(R.id.ibtn_baseactivity_right);
    }


    /**
     * 隐藏标题栏所有组件，GONE或VISIBLE
     */
    public TitleBarUtils setShowOrHide(Activity activity, boolean isShow) {
        if (activity instanceof AppCompatActivity) {
            AppCompatActivity aact = (AppCompatActivity) activity;
            ActionBar actionBar = aact.getSupportActionBar();
            if (actionBar != null) {
                if (isShow) {
                    actionBar.show();
                } else {
                    actionBar.hide();
                }
            } else {
                activity.getWindow().getDecorView().findViewById(R.id.layout_navigationBar)
                        .setVisibility(isShow ? View.VISIBLE : View.GONE);
            }
        } else {
            activity.getWindow().getDecorView().findViewById(R.id.layout_navigationBar)
                    .setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
        return this;
    }


    /**
     * 隐藏线，GONE或VISIBLE
     */
    public TitleBarUtils setShowOrHideLine(Activity activity, boolean isShow) {
        activity.getWindow().getDecorView().findViewById(R.id.title_bottom_line)
                .setVisibility(isShow ? View.VISIBLE : View.GONE);
        return mTitleBarUtils;
    }

    /**
     * 隐藏线，GONE或VISIBLE
     */
    public TitleBarUtils setShowOrHideLine(View view, boolean isShow) {
        view.findViewById(R.id.title_bottom_line)
                .setVisibility(isShow ? View.VISIBLE : View.GONE);
        return mTitleBarUtils;
    }


    /**
     * 设置标题栏颜色
     */
    public TitleBarUtils setTitleBackgroud(Activity activity, int backgroundImgResId) {
        activity.getWindow().getDecorView().findViewById(R.id.layout_navigationBar)
                .setBackground(activity.getResources().getDrawable(backgroundImgResId));
        return mTitleBarUtils;
    }

    /**
     * 设置标题栏颜色
     */
    public TitleBarUtils setTitleBackgroud(View view, int backgroundImgResId) {
        view.findViewById(R.id.layout_navigationBar)
                .setBackground(view.getResources().getDrawable(backgroundImgResId));
        return mTitleBarUtils;
    }

    /**
     * 设置 从右边数，第两个按钮
     */
    public TitleBarUtils setRightSecondBtn(Activity activity, @DrawableRes int secondRes, View.OnClickListener secondOnclick) {
        ImageButton secondImageButton = activity.getWindow().getDecorView().findViewById(R.id.ibtn_two_right);

        secondImageButton.setVisibility(View.VISIBLE);
        secondImageButton.setImageResource(secondRes);

        secondImageButton.setOnClickListener(secondOnclick);
        return this;
    }

    public TitleBarUtils addTabs(final BaseActivity activity, ViewPager viewPager, String tab1, Fragment fragment1, String tab2, Fragment fragment2, ViewPager.OnPageChangeListener listener) {
        //设置标题不可见
        TextView titleView = activity.getWindow().getDecorView().findViewById(R.id.txt_baseactivity_title);
        titleView.setVisibility(View.GONE);
        //设置tab可见
        TabLayout tabLayout = activity.getWindow().getDecorView().findViewById(R.id.title_tab);
        tabLayout.setVisibility(View.VISIBLE);

        TitleFragmentListAdapter adapter = new TitleFragmentListAdapter(activity.getSupportFragmentManager());
        adapter.add(fragment1, tab1);
        adapter.add(fragment2, tab2);

//        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(adapter);

        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
        tabLayout.setupWithViewPager(viewPager);
        viewPager.addOnPageChangeListener(listener);
//        CommonUtil.getInstance().reflex(tabLayout, activity);
        return this;
    }

    /**
     * 设置 从右边数，第两个按钮
     */
    public TitleBarUtils setRightSecondBtn(View view, @DrawableRes int secondRes, View.OnClickListener secondOnclick) {
        ImageButton secondImageButton = view.findViewById(R.id.ibtn_two_right);

        secondImageButton.setVisibility(View.VISIBLE);
        secondImageButton.setImageResource(secondRes);

        secondImageButton.setOnClickListener(secondOnclick);
        return this;
    }

    public TitleBarUtils addTabs(final BaseFragment baseFragment, ViewPager viewPager, String tab1, Fragment fragment1, String tab2, Fragment fragment2, ViewPager.OnPageChangeListener listener) {
        //设置标题不可见
        TextView titleView = baseFragment.findViewById(R.id.txt_baseactivity_title);
        titleView.setVisibility(View.GONE);
        //设置tab可见
        TabLayout tabLayout = baseFragment.findViewById(R.id.title_tab);
        tabLayout.setVisibility(View.VISIBLE);

        TitleFragmentListAdapter adapter = new TitleFragmentListAdapter(baseFragment.getChildFragmentManager());
        adapter.add(fragment1, tab1);
        adapter.add(fragment2, tab2);

//        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(adapter);

        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
        tabLayout.setupWithViewPager(viewPager);
        viewPager.addOnPageChangeListener(listener);
//        CommonUtil.getInstance().reflex(tabLayout, activity);
        return this;
    }

    public TitleBarUtils addMsgTab(final BaseActivity activity, final ViewPager viewPager) {
        //设置标题不可见
        TextView titleView = activity.getWindow().getDecorView().findViewById(R.id.txt_baseactivity_title);
        titleView.setVisibility(View.GONE);
        //设置tab可见
        View msg_title_tab = activity.getWindow().getDecorView().findViewById(R.id.msg_title_tab);
        msg_title_tab.setVisibility(View.VISIBLE);
        final TextView[] textViews = new TextView[2];
        textViews[0] = activity.getWindow().getDecorView().findViewById(R.id.title_tab_tv_0);
        textViews[1] = activity.getWindow().getDecorView().findViewById(R.id.title_tab_tv_1);
        textViews[0].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(0);
            }
        });
        textViews[1].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(1);
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            private int lastPosition = 0;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                TextView lastTextView = textViews[lastPosition];
                TextView curTextView = textViews[position];
                curTextView.setTextColor(Color.parseColor("#333333"));
                lastTextView.setTextColor(Color.parseColor("#666666"));
                curTextView.setTypeface(Typeface.DEFAULT_BOLD);
                lastTextView.setTypeface(Typeface.DEFAULT);

                lastPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        return this;
    }

    /**
     * 设置最后的textView的文本，颜色与点击事件
     *
     * @param activity
     * @param text            要显示的文本
     * @param color           要显示的颜色
     * @param onClickListener 点击事件
     * @return
     */
    public TitleBarUtils setEndTv(Activity activity, String text, int color, View.OnClickListener onClickListener) {
        TextView textView = activity.getWindow().getDecorView().findViewById(R.id.title_end_tv);
        if (textView != null) {
            textView.setVisibility(View.VISIBLE);
            ViewUtil.setText(textView, text);
            if (color > 0)
                textView.setTextColor(color);
            if (onClickListener != null)
                textView.setOnClickListener(onClickListener);
        }
        return this;
    }

    public interface TextChangedListener {
        void onTextChanged(String text);
    }
}
