package com.yyrh.core.impl.login.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.yyrh.core.entity.ClickType;
import com.yyrh.core.entity.Session;
import com.yyrh.core.impl.login.LoginActivity;
import com.yyrh.core.impl.login.LoginFragmentTag;
import com.yyrh.factory.SdkManager;
import com.yyrh.ui.dialog.TipsToast;
import com.yyrh.ui.widget.EventEditText;
import com.yyrh.utils.SessionUtils;

import java.util.ArrayList;

import cn.yyxx.support.ResUtils;

/**
 * @author #Suyghur.
 * Created on 2021/10/19
 */
public class AccountLauncherFragment extends Fragment implements View.OnClickListener {

    private View view;
    private LayoutInflater mInflater;
    private LoginActivity loginImpl;
    private EventEditText eetAccount, eetPwd;
    private Button btnLauncher, btnRegister;
    private ImageView ivCheck;
    private TextView tvAgreement, tvPrivacy;
    private int imgCheck, imgUncheck, imgShow, imgHide, imgUp, imgDown;
    private RelativeLayout rlAccountList;
    private ScrollView svAccountList;
    private LinearLayout llAccountList;
    private ArrayList<Session> userList = new ArrayList<>();
    private boolean check = false;
    private boolean showText = false;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        this.mInflater = inflater;
        if (getActivity() instanceof LoginActivity) {
            loginImpl = (LoginActivity) getActivity();
        }
        if (view == null) {
            view = inflater.inflate(ResUtils.getResId(getActivity(), "hnyy_login_account_launcher", "layout"), container, false);
            initView(view);
        }
        return view;
    }

    private void initView(View view) {

        imgCheck = ResUtils.getResId(getActivity(), "hnyy_check_img", "drawable");
        imgUncheck = ResUtils.getResId(getActivity(), "hnyy_uncheck_img", "drawable");
        imgShow = ResUtils.getResId(getActivity(), "hnyy_show_img", "drawable");
        imgHide = ResUtils.getResId(getActivity(), "hnyy_hide_img", "drawable");
        imgUp = ResUtils.getResId(getActivity(), "hnyy_pick_up_img", "drawable");
        imgDown = ResUtils.getResId(getActivity(), "hnyy_pick_down_img", "drawable");


        eetAccount = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_eet_account", "id"));
        eetAccount.getLeftImageView().setBackgroundResource(ResUtils.getResId(getActivity(), "hnyy_account_img", "drawable"));
        eetAccount.getLeftImageView().setVisibility(View.VISIBLE);
        eetAccount.getEditText().setHint("请输入账号");
        eetAccount.getEditText().setText("test1231");
        eetAccount.getRightImageView().setBackgroundResource(imgDown);
        eetAccount.getRightImageView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeAccountList();
            }
        });

        eetPwd = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_eet_pwd", "id"));
        eetPwd.getLeftImageView().setBackgroundResource(ResUtils.getResId(getActivity(), "hnyy_pwd_img", "drawable"));
        eetPwd.getLeftImageView().setVisibility(View.VISIBLE);
        eetPwd.getEditText().setHint("请输入密码");
        eetPwd.getEditText().setText("a123456");
        eetPwd.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
        eetPwd.getRightImageView().setBackgroundResource(imgShow);
        eetPwd.getRightImageView().setVisibility(View.VISIBLE);
        eetPwd.getRightImageView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!showText) {
                    showText = true;
                    eetPwd.getRightImageView().setBackgroundResource(imgHide);
                    eetPwd.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    showText = false;
                    eetPwd.getRightImageView().setBackgroundResource(imgShow);
                    eetPwd.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                }
            }
        });

        btnLauncher = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_btn_launcher", "id"));
        btnLauncher.setOnClickListener(this);
        btnLauncher.setTag(ClickType.ACTION_LAUNCHER);

        btnRegister = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_btn_quick_register", "id"));
        btnRegister.setOnClickListener(this);
        btnRegister.setTag(ClickType.ACTION_QUICK_REGISTER);

        ivCheck = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_iv_check", "id"));
        ivCheck.setBackgroundResource(imgUncheck);
        ivCheck.setOnClickListener(this);
        ivCheck.setTag(ClickType.ACTION_CHECK);

        tvAgreement = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_tv_agreement", "id"));
        tvAgreement.setOnClickListener(this);
        tvAgreement.setTag(ClickType.ACTION_AGREEMENT);

        tvPrivacy = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_tv_privacy", "id"));
        tvPrivacy.setOnClickListener(this);
        tvPrivacy.setTag(ClickType.ACTION_PRIVACY);

        rlAccountList = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_rl_account_list", "id"));
        svAccountList = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_sv_account_list", "id"));
        llAccountList = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_ll_account_list", "id"));
        rlAccountList.setVisibility(View.GONE);

        autoFillAccountInfo();
    }

    private void autoFillAccountInfo() {
        ArrayList<Session> temp = SessionUtils.getInstance().getLocalSessionLimit3();
        if (temp == null || temp.size() == 0) {
            eetAccount.getRightImageView().setVisibility(View.GONE);
            return;
        }
        for (Session session : temp) {
            if (userList == null) {
                userList = new ArrayList<>();
            }
            userList.add(session);
        }
        if (userList != null && userList.size() > 0) {
            Session session = userList.get(0);
            if (session != null) {
                eetAccount.getEditText().setText(session.userName);
                eetPwd.getEditText().setText(session.pwd);
            }
            if (userList.size() > 1) {
                initAccountList();
                eetAccount.getRightImageView().setVisibility(View.VISIBLE);
            } else {
                eetAccount.getRightImageView().setVisibility(View.GONE);
                rlAccountList.setVisibility(View.GONE);
            }
        } else {
            eetAccount.getRightImageView().setVisibility(View.GONE);
            rlAccountList.setVisibility(View.GONE);
        }
    }

    private void initAccountList() {
        llAccountList.removeAllViews();
        // 单行高
        int height = 100;
        if (eetAccount.getLayoutParams().height != -1 || eetAccount.getLayoutParams().height != 0) {
            height = eetAccount.getLayoutParams().height;
        }
        // ScrollView最大高度
        int maxHeight = 0;
        if (userList != null && userList.size() > 0) {
            if (userList.size() < 3) {
                maxHeight = userList.size() * height;
            } else {
                maxHeight = (userList.size() - 1) * height;
            }
        }

        // 循环添加列
        for (int i = 0; i < userList.size(); i++) {
            Session session = userList.get(i);
            // 添加列线
            LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2);
            View line = new View(getActivity());
            line.setLayoutParams(lineParams);
            line.setBackgroundColor(Color.parseColor("#4D505050"));
            if (llAccountList.getChildCount() > 0) {
                llAccountList.addView(line);
            }
            // 添加账号
            View view = mInflater.inflate(ResUtils.getResId(getActivity(), "hnyy_account_list_item", "layout"), null);
            view.setOnClickListener(new AccountItemClick(session));

            TextView tvAccountName = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_tv_name", "id"));
            tvAccountName.setGravity(Gravity.CENTER_VERTICAL);
            tvAccountName.setPadding(15, 0, 0, 0);
            tvAccountName.setText(session.userName);

            ImageView ivDelete = view.findViewById(ResUtils.getResId(getActivity(), "hnyy_iv_delete", "id"));
            ivDelete.setOnClickListener(new AccountItemDeleteClick(session));

            view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
            llAccountList.addView(view, llAccountList.getChildCount());
        }
        svAccountList.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, maxHeight));
    }

    private void hideAccountList() {
        if (userList != null && userList.size() > 0 && rlAccountList.getVisibility() == View.VISIBLE) {
            eetAccount.getRightImageView().setBackgroundResource(imgDown);
            rlAccountList.setVisibility(View.GONE);
        }
    }

    private void changeAccountList() {
        if (rlAccountList.getVisibility() == View.GONE) {
            eetAccount.getRightImageView().setBackgroundResource(imgUp);
            rlAccountList.setVisibility(View.VISIBLE);
        } else {
            eetAccount.getRightImageView().setBackgroundResource(imgDown);
            rlAccountList.setVisibility(View.GONE);
        }
    }


    private void changeCheck() {
        if (check) {
            check = false;
            ivCheck.setBackgroundResource(imgUncheck);
        } else {
            check = true;
            ivCheck.setBackgroundResource(imgCheck);
        }
    }

    private void accountLauncher() {
        final String userName = eetAccount.getEditText().getText().toString();
        final String pwd = eetPwd.getEditText().getText().toString();
        if (TextUtils.isEmpty(userName)) {
            TipsToast.showErrorMsg(getActivity(), "用户名不能为空");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            TipsToast.showErrorMsg(getActivity(), "密码不能为空");
            return;
        }
        if (!check) {
            TipsToast.showErrorMsg(getActivity(), "请您先勾选同意下方协议");
            return;
        }
        loginImpl.viewModel.accountLauncher(getActivity(), userName, pwd);
    }


    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        switch ((Integer) v.getTag()) {
            case ClickType.ACTION_LAUNCHER:
                accountLauncher();
                break;
            case ClickType.ACTION_QUICK_REGISTER:
                loginImpl.switchFragment(LoginFragmentTag.QUICK_REGISTER);
                break;
            case ClickType.ACTION_CHECK:
                changeCheck();
                break;
            case ClickType.ACTION_AGREEMENT:
                loginImpl.showAgreementDialog(SdkManager.defaultManager().initBean.agreementPrivacy);
                break;
            case ClickType.ACTION_PRIVACY:
                loginImpl.showAgreementDialog(SdkManager.defaultManager().initBean.agreementUser);
                break;
        }
    }

    private class AccountItemClick implements View.OnClickListener {

        private Session session = null;

        AccountItemClick(Session session) {
            this.session = session;
        }

        @Override
        public void onClick(View v) {
            eetAccount.getRightImageView().setBackgroundResource(imgUp);
            hideAccountList();
            eetAccount.getEditText().setText(session.userName);
            eetPwd.getEditText().setText(session.pwd);
        }
    }

    private class AccountItemDeleteClick implements View.OnClickListener {

        private Session session = null;

        AccountItemDeleteClick(Session session) {
            this.session = session;
        }

        @Override
        public void onClick(View v) {
            SessionUtils.getInstance().removeSession(session.userId);
            userList.clear();
            autoFillAccountInfo();
        }
    }
}
