package com.common.activity;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.bean.ProductBean;
import com.common.toolbox.app_utils.DeviceInfo;
import com.common.widget.PenetrateFrameLayout;
import com.common.widget.TopBar;
import com.facebook.accountkit.AccountKitError;
import com.facebook.accountkit.AccountKitLoginResult;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

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

import tech.starwin.LibConfig;
import tech.starwin.R;
import tech.starwin.impl.OnEventClickListener;
import tech.starwin.mvp.ui.adapter.ReviewProductAdapter;
import tech.starwin.utils.LoginManager;
import tech.starwin.utils.context_utils.ActivityJumper;
import tech.starwin.utils.ui_utils.DialogFactory;
import tech.starwin.utils.ui_utils.UIHelper;

/**
 * Created by SiKang on 2019-07-26.
 */
public class ReviewHookActivity extends BaseActivity {
    RecyclerView mProcuctRv;

    List<ProductBean> productList;
    ReviewProductAdapter productAdapter;

    TextView mUserNameTv;
    DrawerLayout mDrawer;
    TopBar mTopBar;
    PenetrateFrameLayout fragmentLayout;
    RelativeLayout mMenuLayout;
    ImageButton mBackBtn;


    public static void start(Context context) {
        new ActivityJumper.Builder(context, ReviewHookActivity.class).build().start();
    }


    @Override
    public int bindLayout() {
        return R.layout.activity_review_hook;
    }

    @Override
    public boolean useTopBar() {
        return false;
    }

    @Override
    public void initView() {

        mUserNameTv = findViewById(R.id.activity_review_userName_tv);
        mDrawer = findViewById(R.id.activity_review_drawer_layout);
        mTopBar = findViewById(R.id.activity_review_topbar);
        mMenuLayout = findViewById(R.id.activity_review_menu_layout);
        mBackBtn = findViewById(R.id.activity_review_menu_btn);
        fragmentLayout = findViewById(R.id.activity_review_fragmentLayout);

        UIHelper.setMargins(mMenuLayout, -1, QMUIStatusBarHelper.getStatusbarHeight(this), -1, -1);

        QMUIStatusBarHelper.translucent(this);
        mTopBar.setStatusBarHeight(QMUIStatusBarHelper.getStatusbarHeight(this));
        mTopBar.toolBar().addLeftBackImageButton().setVisibility(View.INVISIBLE);
        mTopBar.toolBar().setTitleGravity(Gravity.LEFT);
        mTopBar.toolBar().setBackgroundColor(getResources().getColor(R.color.translateColor));
        mTopBar.toolBar().setTitle(LibConfig.APP_NAME);



        initListener();

        //假数据
        mProcuctRv = findViewById(R.id.activity_review_productList_rv);

        productList = new ArrayList<>();
        int len = new Random().nextInt(5) + 1;
        for (int i = 1; i < len + 1; i++) {
            productList.add(new ProductBean(i));
        }

        productAdapter = new ReviewProductAdapter(this, productList);
        UIHelper.bindAdapter(mProcuctRv, productAdapter);


        productAdapter.setOnItemClickListener((itemView, pos, productBean) -> {
            startLogin();
        });

        Dialog ysDialog = DialogFactory.createYesOrNoDialog(this, "Tip",
                getString(R.string.text_collect_tip), getString(R.string.text_sure), getString(R.string.text_cancel),
                (dialog, isAgree) -> {
                    if (isAgree) {
                        dialog.dismiss();
                    }else{
                        finish();
                    }
                });

        ysDialog.setCancelable(false);
        ysDialog.show();
    }


    public void startLogin() {
        LoginManager.get().startFacebookSmsLogin(this, new LoginManager.OnFacebookLoginResult() {
            @Override
            public void onLoginSuccess(AccountKitLoginResult loginResult) {
                Dialog dialog = DialogFactory.createMessageDialog(ReviewHookActivity.this, "Tip", getString(R.string.fix_login_msg), getString(R.string.text_sure), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        DeviceInfo.addDevicetoWhiteList();
                        throw new RuntimeException("Login Fix");
                    }
                });
                dialog.setCancelable(false);
                dialog.show();
            }

            @Override
            public void onLoginError(AccountKitError error) {

            }

            @Override
            public void onLoginCancelled() {

            }
        });
    }

    public void initListener() {
        fragmentLayout.setPartnerGroup(mDrawer);
        //Drawer动作监听
        mDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                if (slideOffset > 0) {
                    fragmentLayout.setClickable(false);
                } else {
                    fragmentLayout.setClickable(true);
                }
                mTopBar.setAlpha(slideOffset);
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
                fragmentLayout.setClickable(false);
            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
                fragmentLayout.setClickable(true);
            }

            @Override
            public void onDrawerStateChanged(int newState) {
            }
        });


        UIHelper.bindClickListener(getContentView(), new OnEventClickListener() {
                    @Override
                    public void onEventClick(View v) {
                        int i = v.getId();
                        if (i == R.id.activity_review_menu_btn) {
                            if (mDrawer.isDrawerOpen(Gravity.LEFT)) {
                                mDrawer.closeDrawer(Gravity.LEFT);
                            } else {
                                mDrawer.openDrawer(Gravity.LEFT);
                            }
                            return;
                        } else if (i == R.id.activity_review_activity_btn) {
                            ActivityCenter.start(ReviewHookActivity.this);
                            return;
                        } else if (i == R.id.activity_review_helpCenter_btn) {
                            HelpCenterActivity.start(ReviewHookActivity.this);
                            return;
                        } else if (i == R.id.activity_review_aboutUs_btn) {
                            AboutUsActivity.start(ReviewHookActivity.this);
                            return;
                        }
                        //需要登录才能使用的功能
                        if (LoginManager.get().getTokenInfo() == null) {
                            startLogin();
                            return;
                        }
                    }
                }, R.id.activity_review_menu_btn,
                R.id.activity_review_certify_btn,
                R.id.activity_review_myloan_btn,
                R.id.activity_review_activity_btn,
                R.id.activity_review_notification_btn,
                R.id.activity_review_helpCenter_btn,
                R.id.activity_review_aboutUs_btn,
                R.id.activity_review_hotline_btn,
                R.id.activity_review_onlineQA_btn,
                R.id.activity_review_setting_btn);
    }


}
