/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.onecable.admin.view.things;


import com.ntels.onecable.admin.R;
import com.ntels.onecable.admin.common.AppConfig;
import com.ntels.onecable.admin.common.Constants;
import com.ntels.onecable.admin.common.c_interface.ListInterface;
import com.ntels.onecable.admin.common.util.LogUtil;
import com.ntels.onecable.admin.common.util.Util;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.HashMap;

import static com.ntels.onecable.admin.common.c_interface.ListInterface.ON_CALL_ACTION_EXECUTE;


/**
 * <pre>
 * 1. ClassName :   ThingsDetailVpAdapter
 * 2. FileName  :   ThingsDetailVpAdapter.java
 * 3. Package   :   com.ntels.onecable.view.things
 * 4. Date      :   2016-11-25
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
class ThingsDetailVpAdapter extends PagerAdapter {

    private static final String TAG = ThingsDetailVpAdapter.class.getSimpleName();

    private ListInterface mAdapterCallback;
    private Context mContext;
    private Util mUtil = new Util();
    private AppConfig mAppConfig;
    private String mHttpValue = "";
    private int mCurrentPosition = -1;
    private boolean mIsHideDivider = false;
    private String mDeviceId = "";

    private SparseArray<View> mViews = new SparseArray<>();
    private ArrayList<HashMap<String, Object>> mTotalList;


    /**
     * 생성자.
     *
     * @param context         컨텍스트
     * @param alval           data
     * @param adapterCallback 콜백 object
     */
    ThingsDetailVpAdapter(final Context context, String deviceId, ArrayList<HashMap<String, Object>> alval,
                          ListInterface adapterCallback) {

        this.mContext = context;
        mDeviceId = deviceId;
        mTotalList = alval;
        mAppConfig = (AppConfig) this.mContext.getApplicationContext();
        try {
            this.mAdapterCallback = adapterCallback;
        } catch (ClassCastException ex) {
            ex.printStackTrace();
            //            throw new ClassCastException("Activity must implement AdapterCallback.");
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        View view = (View) object;
        ViewPager viewPager = (ViewPager) container;
        viewPager.removeView(view);
        mViews.remove(position);
    }

    @Override
    public int getItemPosition(Object object) {

        return POSITION_NONE;
    }

    @Override
    public int getCount() {

        return mTotalList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {

        return view.equals(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        HashMap<String, Object> hmItem;
        hmItem = mTotalList.get(position);

        String httpOnlineState = mUtil.null2string(hmItem.get(Constants.KEY_ONLINE_STATE));
        String httpCmdclsName = mUtil.null2string(hmItem.get(Constants.KEY_CMDCLS_NAME));
        String httpUnit = mUtil.null2string(hmItem.get(Constants.KEY_UNIT));
        String httpCmdclsTypeId = mUtil.null2string(hmItem.get(Constants.KEY_CMDCLS_TYPE_ID));
        String httpContentValue = mUtil.null2string(hmItem.get(Constants.KEY_CONTENT_VALUE));
        String httpContentLastDatetime = mUtil.UTCtoLocal5(mUtil.null2string(hmItem.get(Constants.KEY_CONTENT_LAST_DATETIME)));

        LogUtil.i(TAG, "KEY_content_value1 : " + httpContentValue);

        String tempValue;
        String select = mUtil.getStringValues(mContext, "CMDCLS_TYPE_ID_" + httpCmdclsTypeId + "_BG_DEFAULT");
        if ("".equals(httpContentValue)) {
            tempValue = mUtil.getStringValues(mContext, "CMDCLS_TYPE_ID_" + httpCmdclsTypeId + "_" + select);
            httpContentValue = select;
        } else {
            tempValue = mUtil.getStringValues(mContext, "CMDCLS_TYPE_ID_" + httpCmdclsTypeId + "_" + httpContentValue);
        }

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout childView = (LinearLayout) inflater.inflate(R.layout.row_things_detail_list, null);

        mUtil.getView(childView, R.id.llRowBody1).setVisibility(View.VISIBLE);
        mUtil.getView(childView, R.id.llRowBody2).setVisibility(View.GONE);
        mUtil.getView(childView, R.id.tvRowBody1_Lasttime).setVisibility(View.GONE);
        mUtil.getView(childView, R.id.llRowBody1_Control).setVisibility(View.GONE);
        mUtil.getView(childView, R.id.btnRowBody1_Contents_On).setVisibility(View.GONE);
        mUtil.getView(childView, R.id.btnRowBody1_Contents_Off).setVisibility(View.GONE);


        final View vLoding = mUtil.getView(childView, R.id.llLoading1);
        String httpRequestTime = mUtil.null2string(hmItem.get(Constants.KEY_REQUEST_TIME));
        if (!"".equals(httpRequestTime)) {
            LogUtil.i(TAG, "httpRequestTime - " + httpRequestTime);
            vLoding.setVisibility(View.VISIBLE);
        } else {
            vLoding.setVisibility(View.GONE);
        }

        int tempType = 0;
        int cmdclsType = mUtil.null2int(hmItem.get(Constants.KEY_CMDCLS_TYPE_ID));
        switch (cmdclsType) {
            case Constants.CMDCLS_TYPE_ID_36002:
                //가스락
                mUtil.getTextView(childView, R.id.tvRowBody1_Title).setText(tempValue);
                if ("OFF".equals(httpOnlineState) || mAppConfig.getSelectCtrtGroupHomehubOnlineState(mDeviceId).equals("OFF")) {
                    setDispOffline(childView, R.drawable.img_things_detail_05_00_valve_state_offline);
                } else {
                    if (httpContentValue.equals(select)) {
                        mUtil.getTextView(childView, R.id.tvRowBody1_Title).setTextColor(Util.getColor(mContext, R.color.color_text_01));
                    } else {
                        mUtil.getView(childView, R.id.llRowBody1_Control).setVisibility(View.VISIBLE);
                        mUtil.getView(childView, R.id.btnRowBody1_Contents_Off).setVisibility(View.VISIBLE);
                        mUtil.getTextView(childView, R.id.tvRowBody1_Title).setTextColor(Util.getColor(mContext, R.color.color_text_02));
                    }
                }
                mUtil.getView(childView, R.id.btnRowBody1_Contents_Off).setTag(position);
                mUtil.getView(childView, R.id.btnRowBody1_Contents_Off).setOnClickListener(new View.OnClickListener() {
                    int tag = 0;

                    @Override
                    public void onClick(View view) {

                        tag = (int) view.getTag();
                        setHttpValue("CLOSE");
                        vLoding.setVisibility(View.VISIBLE);
                        mAdapterCallback.onCallAction(ON_CALL_ACTION_EXECUTE, tag);
                    }
                });
                break;
            //SWITCH_BINARY
            case Constants.CMDCLS_TYPE_ID_17001:
            case Constants.CMDCLS_TYPE_ID_17002:
            case Constants.CMDCLS_TYPE_ID_17005:
                mUtil.getTextView(childView, R.id.tvRowBody1_Title).setText(tempValue);
                if ("OFF".equals(httpOnlineState) || mAppConfig.getSelectCtrtGroupHomehubOnlineState(mDeviceId).equals("OFF")) {
                    setDispOffline(childView, R.drawable.img_things_detail_06_00_plug_state_offline);
                } else {
                    mUtil.getView(childView, R.id.llRowBody1_Control).setVisibility(View.VISIBLE);
                    if (httpContentValue.equals(select)) {
                        mUtil.getView(childView, R.id.btnRowBody1_Contents_On).setVisibility(View.VISIBLE);
                        mUtil.getTextView(childView, R.id.tvRowBody1_Title).setTextColor(Util.getColor(mContext, R.color.color_text_01));
                    } else {
                        mUtil.getView(childView, R.id.btnRowBody1_Contents_Off).setVisibility(View.VISIBLE);
                        mUtil.getTextView(childView, R.id.tvRowBody1_Title).setTextColor(Util.getColor(mContext, R.color.color_text_02));
                    }
                }
                mUtil.getView(childView, R.id.btnRowBody1_Contents_On).setTag(position);
                mUtil.getView(childView, R.id.btnRowBody1_Contents_On).setOnClickListener(new View.OnClickListener() {
                    int tag = 0;

                    @Override
                    public void onClick(View view) {

                        tag = (int) view.getTag();
                        setHttpValue("ON");
                        vLoding.setVisibility(View.VISIBLE);
                        mAdapterCallback.onCallAction(ON_CALL_ACTION_EXECUTE, tag);
                    }
                });
                mUtil.getView(childView, R.id.btnRowBody1_Contents_Off).setTag(position);
                mUtil.getView(childView, R.id.btnRowBody1_Contents_Off).setOnClickListener(new View.OnClickListener() {
                    int tag = 0;

                    @Override
                    public void onClick(View view) {

                        tag = (int) view.getTag();
                        setHttpValue("OFF");
                        vLoding.setVisibility(View.VISIBLE);
                        mAdapterCallback.onCallAction(ON_CALL_ACTION_EXECUTE, tag);
                    }
                });
                break;
            case Constants.CMDCLS_TYPE_ID_11011:
                //문열림 센서
                mUtil.getTextView(childView, R.id.tvRowBody1_Title).setText(tempValue);
                mUtil.getTextView(childView, R.id.tvRowBody1_Lasttime).setVisibility(View.VISIBLE);
                mUtil.getTextView(childView, R.id.tvRowBody1_Lasttime).setText(httpContentLastDatetime);
                if ("OFF".equals(httpOnlineState) || mAppConfig.getSelectCtrtGroupHomehubOnlineState(mDeviceId).equals("OFF")) {
                    setDispOffline(childView, R.drawable.img_things_detail_01_00_sensor_door_offline);
                } else {
                    if (httpContentValue.equals(select)) {
                        mUtil.getTextView(childView, R.id.tvRowBody1_Title).setTextColor(Util.getColor(mContext, R.color.color_text_01));
                    } else {
                        mUtil.getTextView(childView, R.id.tvRowBody1_Title).setTextColor(Util.getColor(mContext, R.color.color_text_02));
                    }
                }
                break;
        }

        container.addView(childView);
        mViews.put(position, childView);

        return childView;
    }

    public void setDispOffline(View view) {

        mUtil.getTextView(view, R.id.tvRowBody1_Title).setTextColor(Util.getColor(mContext, R.color.color_text_01));
        mUtil.getTextView(view, R.id.tvRowBody1_Title).setText(mUtil.getString(mContext, R.string.layout_offline));
        mUtil.getView(view, R.id.llRowBody1_Control).setVisibility(View.GONE);
    }

    public void setDispOffline(View view, int imageId) {

//        mUtil.getView(view, R.id.ivRowBody1_Icon).setBackgroundResource(imageId);
        mUtil.getTextView(view, R.id.tvRowBody1_Title).setTextColor(Util.getColor(mContext, R.color.color_text_01));
        mUtil.getTextView(view, R.id.tvRowBody1_Title).setText(mUtil.getString(mContext, R.string.layout_offline));
        mUtil.getView(view, R.id.llRowBody1_Control).setVisibility(View.GONE);
    }

    public String getHttpValue() {

        return mHttpValue;
    }

    public void setHttpValue(String val) {

        mHttpValue = val;
    }

    public boolean isHideDivider() {
        return mIsHideDivider;
    }

    public void setHideDivider(boolean hideDivider) {
        mIsHideDivider = hideDivider;
    }
}
