package com.tofly.ghrq_route.common.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.DialogTitle;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lcp.arecyclerview.widget.ARecycleView;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.module.user.controller.UserCallBack;
import com.tofly.ghrq_route.module.user.controller.UserController;
import com.tofly.ghrq_route.module.user.domain.CarBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.ui.adapter.CarListAdapter;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/2/13.
 */

public class UserBindCarDailog extends AlertDialog {
    @BindView(android.R.id.icon)
    ImageView icon;
    @BindView(R.id.alertTitle)
    DialogTitle alertTitle;
    @BindView(R.id.layout_input1)
    LinearLayout layoutInput1;
    @BindView(R.id.text_hint1)
    TextView textHint1;
    @BindView(R.id.recycler_view)
    ARecycleView recyclerView;
    @BindView(android.R.id.button2)
    Button button2;
    @BindView(android.R.id.button1)
    Button button1;
    private Context mContext;
    private String mUserToken;
    private UserController userController;
    private List<CarBean> mCarList;
    private CarListAdapter carListAdapter;
    private ClickListenerInterface mClickListenerInterface;

    public UserBindCarDailog(Context context) {
        super(context, R.style.AlertDialog);
        mContext = context;
    }

    public interface ClickListenerInterface {
        public void doOk(String name);
        public void doCancel();
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.mClickListenerInterface = clickListenerInterface;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_user_bind_car);
        ButterKnife.bind(this);

        initView();
        initData();
    }


    private void initView() {
        alertTitle.setText(R.string.user_bindcar_title);
        textHint1.setText(R.string.bindcar_input_carno);
        button1.setText(android.R.string.ok);
        button2.setText(android.R.string.cancel);
        recyclerView.setEnabled(false);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.height = (int) (d.heightPixels * 0.5); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    private void initData(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        List carlist = new ArrayList<CarBean>();
        carListAdapter = new CarListAdapter(mContext, carlist);
        recyclerView.setAdapter(carListAdapter);
        recyclerView.setRefreshing(true);
        String noData = mContext.getResources().getString(R.string.bindcar_empty_data);
        recyclerView.setEmptyViewText(noData);
        getUserCarList();
    }

    private void getUserCarList(){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null){
            if (userController == null)
                userController = new UserController(mContext);
            userController.getCarList(userBean.getToken(), new UserCallBack(){
                @Override
                public void onGetCarList(boolean isSuccess, int errorCode, String errMsg, List<CarBean> carList) {
                    super.onGetCarList(isSuccess, errorCode, errMsg, carList);
                    recyclerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            recyclerView.setRefreshing(false);
                            recyclerView.hideMoreProgress();
                        }
                    }, 2000);
                    if (isSuccess && carList != null){
                        if (carList.size() >= 0){
                            recyclerView.setTotal(carList.size());
                            carListAdapter.clear();
                            carListAdapter.addAll(carList);

                            Window dialogWindow = getWindow();
                            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                            DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
                            lp.height = (int) (d.heightPixels * 0.8); // 高度设置为屏幕的0.6
                            if (carList.size() < 4){
                                lp.height = (int) (d.heightPixels * 0.5); // 高度设置为屏幕的0.6
                            }
                            dialogWindow.setAttributes(lp);
                        }
                    }else {
                        ToastUtil.showToast(mContext, errMsg);
                    }
                }
            });
        }
    }

    @OnClick({android.R.id.button1, android.R.id.button2})
    public void onClick(View view) {
        switch (view.getId()) {
            /*Button OK*/
            case android.R.id.button1:
                CarBean carBean = null;
                if (carListAdapter != null){
                     carBean = carListAdapter.getSelectedItem();
                }
                if (mClickListenerInterface != null){
                    if (carBean != null){
                        String carno = carBean.getCarid();
                        mClickListenerInterface.doOk(carno);
                    }else {
                        mClickListenerInterface.doCancel();
                    }
                }
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                this.dismiss();
                break;
            case android.R.id.button2:
                if (mClickListenerInterface != null){
                    mClickListenerInterface.doCancel();
                }
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                this.dismiss();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (mClickListenerInterface != null){
            mClickListenerInterface.doCancel();
        }
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        this.dismiss();
    }
}
