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

package com.ntels.onecable.view.dialog;

import com.ntels.onecable.R;
import com.ntels.onecable.common.Constants;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.widget.CustomActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * <pre>
 * 1. ClassName :   DialogSpinner
 * 2. FileName  :   DialogSpinner.java
 * 3. Package   :   com.ntels.onecable.view.dialog
 * 4. Date      :   2016-12-01
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class DialogSpinner extends CustomActivity implements View.OnClickListener {

    private static final String TAG = DialogSpinner.class.getSimpleName();
    Intent intent;
    Button[] btnCheck;
    ArrayList<String> titleList = new ArrayList<>();
    ArrayList<String> titleSubList = new ArrayList<>();
    ArrayList<String> iconList = new ArrayList<>();
    ArrayList<String> listResult = new ArrayList<>();
    private Activity act;
    private Util util = new Util();
    private String title = "";
    private String btn1 = "";
    private String btn2 = "";
    private int position = -1;
    private int type = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        act = DialogSpinner.this;

        View viewToLoad = LayoutInflater.from(act).inflate(R.layout.dialog_spinner, null);
        this.setContentView(viewToLoad);

        if (savedInstanceState != null) {
            Log.i("DialogSpinner", "savedInstanceState is not null");
            title = savedInstanceState.getString(Constants.EXTRA_SPINNER_TITLE).substring(4);
            titleList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST1);
            titleSubList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST2);
            iconList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_ICON_LIST);
            listResult = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_POSITION_LIST);
            // 0: 세로 한개 선택, 1: 가로 한개선택, 2: 세로 여러개,
            type = savedInstanceState.getInt(Constants.EXTRA_SPINNER_TYPE, 0);
            btn1 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN1);
            btn2 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN2);
            position = savedInstanceState.getInt(Constants.EXTRA_SPINNER_POSITION, -1);
        } else {
            Intent intent = getIntent();
            title = intent.getStringExtra(Constants.EXTRA_SPINNER_TITLE);
            titleList = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_LIST1);
            titleSubList = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_LIST2);
            iconList = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_ICON_LIST);
            listResult = intent.getStringArrayListExtra(Constants.EXTRA_SPINNER_POSITION_LIST);
            // 0: 세로 한개 선택, 1: 가로 한개선택, 2: 세로 여러개,
            type = intent.getIntExtra(Constants.EXTRA_SPINNER_TYPE, 0);
            btn1 = intent.getStringExtra(Constants.EXTRA_SPINNER_BTN1);
            btn2 = intent.getStringExtra(Constants.EXTRA_SPINNER_BTN2);
            position = intent.getIntExtra(Constants.EXTRA_SPINNER_POSITION, -1);
        }
        if (title == null) {
            title = util.getString(act, R.string.layout_selected);
        }

        util.getView(act, R.id.ivTitleIcon).setVisibility(View.GONE);

        if (btn1 == null) {
            btn1 = util.getString(act, R.string.layout_selected);
        }
        if (btn2 == null) {
            btn2 = util.getString(act, R.string.layout_cancel);
        }
        util.getTextView(act, R.id.tvTitle).setText(title);
        util.getTextView(act, R.id.tvBtn1).setText(btn1);
        util.getTextView(act, R.id.tvBtn2).setText(btn2);
        util.getView(act, R.id.llBtn1).setOnClickListener(this);
        util.getView(act, R.id.llBtn2).setOnClickListener(this);
        setList();
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {

        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
            title = savedInstanceState.getString(Constants.EXTRA_SPINNER_TITLE);
            titleList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST1);
            titleSubList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST2);
            iconList = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_ICON_LIST);
            listResult = savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_POSITION_LIST);
            // 0: 세로 한개 선택, 1: 가로 한개선택
            type = savedInstanceState.getInt(Constants.EXTRA_SPINNER_TYPE, 0);
            btn1 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN1);
            btn2 = savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN2);
            position = savedInstanceState.getInt(Constants.EXTRA_SPINNER_POSITION, -1);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
        outState.putString(Constants.EXTRA_SPINNER_TITLE, title);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_LIST1, titleList);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_LIST2, titleSubList);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_ICON_LIST, iconList);
        outState.putStringArrayList(Constants.EXTRA_SPINNER_POSITION_LIST, listResult);
        // 0: 세로 한개 선택, 1: 가로 한개선택
        outState.putInt(Constants.EXTRA_SPINNER_TYPE, type);
        outState.putString(Constants.EXTRA_SPINNER_BTN1, btn1);
        outState.putString(Constants.EXTRA_SPINNER_BTN2, btn2);
        outState.putInt(Constants.EXTRA_SPINNER_POSITION, position);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    /**
     * dialog에 childView 붙이는 작업.
     */
    private void setList() {

        LinearLayout llChildView = null;
        LinearLayout llBody = null;

        if (type == 0) {
            findViewById(R.id.llBody1).setVisibility(View.VISIBLE);
            findViewById(R.id.llBody2).setVisibility(View.GONE);
            ((LinearLayout) findViewById(R.id.llBodyList)).removeAllViews();
            llBody = util.getLinearLayout(act, R.id.llBodyList);
        } else if (type == 1) {
            findViewById(R.id.llBody1).setVisibility(View.GONE);
            findViewById(R.id.llBody2).setVisibility(View.VISIBLE);
            ((LinearLayout) findViewById(R.id.llBody2)).removeAllViews();
            llBody = util.getLinearLayout(act, R.id.llBody2);
        } else if (type == 2) {
            findViewById(R.id.llBody1).setVisibility(View.VISIBLE);
            findViewById(R.id.llBody2).setVisibility(View.GONE);
            ((LinearLayout) findViewById(R.id.llBodyList)).removeAllViews();
            llBody = util.getLinearLayout(act, R.id.llBodyList);
        }
        if (titleList.size() > 0) {
            btnCheck = new Button[titleList.size()];
            for (int row = 0; row < titleList.size(); row++) {
                if (type == 0) {
                    llChildView = (LinearLayout) View.inflate(this, R.layout.dialog_spinner_row1, null);
                    ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(titleList.get(row));
                } else if (type == 1) {
                    llChildView = (LinearLayout) View.inflate(this, R.layout.dialog_spinner_row2, null);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
                    params.weight = 1.0f;
                    llChildView.setLayoutParams(params);
                } else if (type == 2) {
                    llChildView = (LinearLayout) View.inflate(this, R.layout.dialog_spinner_row3, null);
                }
                llBody.addView(llChildView);
                btnCheck[row] = (Button) llChildView.findViewById(R.id.btnCheck);
                if (type == 1) {
                    ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(titleList.get(row));
                } else {
                    if (titleSubList == null) {
                        ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(titleList.get(row));
                        llChildView.findViewById(R.id.tvBody2).setVisibility(View.GONE);
                    } else {
                        ((TextView) llChildView.findViewById(R.id.tvBody1)).setText(titleList.get(row));
                        llChildView.findViewById(R.id.tvBody2).setVisibility(View.VISIBLE);
                        ((TextView) llChildView.findViewById(R.id.tvBody2)).setText(titleSubList.get(row));
                    }
                    if (iconList == null) {
                        llChildView.findViewById(R.id.ivIcon).setVisibility(View.GONE);
                    } else {
                        llChildView.findViewById(R.id.ivIcon).setVisibility(View.VISIBLE);
                        util.setImage(act, iconList.get(row), ((ImageView) llChildView.findViewById(R.id.ivIcon)));
                    }
                }
                btnCheck[row].setTag(row);
                btnCheck[row].setOnClickListener(this);
                llChildView.setTag(row);
                llChildView.setOnClickListener(this);
                if (type != 2) {
                    if (row == position) {
                        btnCheck[row].setSelected(true);
                    }
                }
            }
        }
        if (type == 2) {
            for (int row = 0; row < listResult.size(); row++) {
                btnCheck[util.str2int(listResult.get(row))].setSelected(true);
            }
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.llBtn1:
                intent = new Intent();
                if (type == 2) {
                    listResult = new ArrayList<>();
                    for (int row = 0; row < titleList.size(); row++) {
                        if (btnCheck[row].isSelected()) {
                            listResult.add("" + row);
                        }
                    }
                    intent.putExtra(Constants.EXTRA_SPINNER_RESULT_LIST, listResult);
                } else {
                    intent.putExtra(Constants.EXTRA_SPINNER_RESULT, position);
                }
                act.setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;
            case R.id.llBody:
            case R.id.btnCheck:
                int checkRow = (Integer) view.getTag();
                if (type == 2) {
                    if (view.isSelected()) {
                        view.setSelected(false);
                    } else {
                        view.setSelected(true);
                    }
                } else {
                    for (int row = 0; row < titleList.size(); row++) {
                        if (btnCheck[row].isSelected()) {
                            btnCheck[row].setSelected(false);
                        }
                    }
                    btnCheck[checkRow].setSelected(true);
                    position = checkRow;
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {

        intent = new Intent();
        act.setResult(RESULT_CANCELED, intent);
        finish();
    }
}
