package com.sheep.gamegroup.view.dialog;

import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kfzs.duanduan.utils.dlg.ViewFindUtils;
import com.sheep.gamegroup.model.entity.Agreement;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.ArrayAdapter;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


public class DialogAgreement {
    /**
     * 协议对话框不能取消，只能同意，如果同意后访问接口成功，则通过，否则都算失败，强制关闭后下次重启仍会获取协议列表
     * @param activity
     * @param list
     * @param action1
     * @return
     */
    public static AlertDialog showDialog(final Activity activity, final List<Agreement> list, final Action1<String> action1) {
        View view = View.inflate(activity, R.layout.dialog_agreement, null);
        final AlertDialog mAlertDialog = new AlertDialog.Builder(activity, R.style.MyDialogActivityTheme)
                .setView(view)
                .create();
        final TextView dialog_title = ViewFindUtils.find(view, R.id.dialog_title);
        final TextView dialog_content = ViewFindUtils.find(view, R.id.dialog_content);

        dialog_title.setText(list.get(0).getName());
//        ViewUtil.loadDataWithBaseURL(dialog_content, list.get(0).getContent());
        dialog_content.setMaxHeight(G.HEIGHT * 2 / 3);
        ViewUtil.setH5Text(dialog_content, list.get(0).getContent());
        final View dialog_sure = ViewFindUtils.find(view, R.id.dialog_sure);
        final List<CheckBox> checkBoxList = ListUtil.emptyList();

        final ListView dialog_center_lv = ViewFindUtils.find(view, R.id.dialog_center_lv);
        final CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                dialog_sure.setEnabled(isAllChecked());
            }

            private boolean isAllChecked() {
                for (CheckBox checkBox : checkBoxList) {
                    if(!checkBox.isChecked())
                        return false;
                }
                return true;
            }
        };
        dialog_center_lv.setAdapter(new ArrayAdapter<Agreement>(activity, R.layout.dialog_agreement_item, list){
            @Override
            public boolean convert(int position, View convertView, ViewGroup parent, final Agreement item) {
                final CheckBox dialog_agreement_item_cb = ViewFindUtils.find(convertView, R.id.dialog_agreement_item_cb);
                final TextView dialog_agreement_item_tv = ViewFindUtils.find(convertView, R.id.dialog_agreement_item_tv);
                dialog_agreement_item_tv.setText(item.getName());
                if(!checkBoxList.contains(dialog_agreement_item_cb))
                    checkBoxList.add(dialog_agreement_item_cb);
                dialog_agreement_item_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(!TextUtils.equals(dialog_title.getText().toString(), item.getName())) {
                            dialog_title.setText(item.getName());
//                            ViewUtil.loadDataWithBaseURL(dialog_content, item.getContent());
                            ViewUtil.setH5Text(dialog_content, item.getContent());
                            dialog_content.scrollTo(0, 0);
                        }
                    }
                });
                dialog_agreement_item_cb.setOnCheckedChangeListener(onCheckedChangeListener);
                return true;
            }
        });


        View.OnClickListener onClickListener =  new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAlertDialog.dismiss();
                JSONArray jsonArray = new JSONArray();
                for (Agreement agreement : list) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("ProtocolId", agreement.getId());
                    jsonArray.add(jsonObject);
                }
                SheepApp.getInstance().getNetComponent().getApiService().agreementBack(jsonArray)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new SheepSubscriber<BaseMessage>(activity) {
                                    @Override
                                    public void onNext(BaseMessage baseMessage) {
                                        action1.call(null);
                                    }

                                    @Override
                                    public void onError(BaseMessage baseMessage) {
                                        action1.call(baseMessage.getErrorMsg());
                                    }
                                });
            }
        };
        dialog_sure.setOnClickListener(onClickListener);
        mAlertDialog.setCancelable(false);
//        mAlertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
//            @Override
//            public void onCancel(DialogInterface dialogInterface) {
//                action1.call("您拒绝了协议");
//            }
//        });
        try {
            mAlertDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mAlertDialog;
    }
}
