Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
89 kB
2
Indexable
Never
class Survey_Fragment : Fragment(), VolleyInterface {
    var topAdverViewArrayList: ArrayList<AdvertiesmentTopView>? = null
    var bottomAdverViewArrayList: ArrayList<AdvertiesMentbottomView>? = null
    var surveyCategoryListings: java.util.ArrayList<SurveyCategoryListing>? = ArrayList()
    var sessionManager: SessionManager? = null
    var str_welcome = ""
    var str_thanku = ""

    var text: String? = null
    var question_id: String? = null
    var question: String? = null
    var question_type: String? = null

    var StrRadio: String? = null
    var stredt: String? = null
    var progress_amount = 0
    var listData: HashMap<String, JSONArray>? = null
    var tempQuestionList: ArrayList<Question> ?= null
    var questionArrayList: ArrayList<Question>? = null
    var arrayAddiChkList: ArrayList<String>? = null
    var jArrayInput: JSONArray? = null
    var jsonInput: JSONObject? = null
    var index = 0
    var q_index = 0
    var total_progres = 0
    var pos = 0
    var ansHashMap: HashMap<String, JSONObject?>? = null
    var split_chk: Array<String>? = null
    var sqLiteDatabaseHandler: SQLiteDatabaseHandler? = null

    // var tag = "SurveyFragment"
    private var TAG = "SurveyFragment"
    var img_survey: ImageView? = null
    var defaultLang: DefaultLang? = null
    var is_view = "0"
    var message = ""
    var categoryId:String?= null
    var isNavigatingBack = false
    var categoryMap = hashMapOf<String, ArrayList<Question>>()
    var bundle : Bundle ?= null

    lateinit var binding:FragmentSurveyBinding

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
//        arguments?.let { bundle ->
//        }
        bundle = arguments
        categoryId = bundle!!.getString("categoryId")
            Log.d("YourFragment", "Received categoryId: $categoryId")
        // Inflate the layout for this fragment
        val rootView = inflater.inflate(R.layout.fragment_survey, container, false)
        GlobalData.IS_SURVEY_VISIBLE=true
        binding = FragmentSurveyBinding.bind(rootView)
        activity!!.title = ""
        (activity as MainActivity?)!!.setDrawerState(false)
        sessionManager = SessionManager(activity)
        defaultLang = sessionManager!!.multiLangString
        binding.webViewStart?.settings?.defaultTextEncodingName = "utf-8"
        binding.webViewThanku.settings?.defaultTextEncodingName = "utf-8"
        topAdverViewArrayList = ArrayList()
        bottomAdverViewArrayList = ArrayList()
        sqLiteDatabaseHandler = SQLiteDatabaseHandler(activity)
        binding.webViewStart.settings?.javaScriptEnabled = true
        binding.webViewStart.settings?.allowContentAccess = true
        binding.webViewStart.isVerticalScrollBarEnabled = true
        binding.webViewStart.isHorizontalScrollBarEnabled = true
        binding.webViewThanku.settings?.javaScriptEnabled = true
        binding.webViewThanku.settings?.allowContentAccess = true
        binding.webViewThanku.isVerticalScrollBarEnabled = true
        binding.webViewThanku.isHorizontalScrollBarEnabled = true
        listData = HashMap()
        ansHashMap = HashMap()
        jArrayInput = JSONArray()
        arrayAddiChkList = ArrayList()
        questionArrayList = ArrayList()
        tempQuestionList = ArrayList()

        SessionManager.strModuleId = sessionManager!!.categoryId
        SessionManager.strMenuId = "15"
        binding.btnStart.text = defaultLang?.get15Start()
        binding.btnFinish.text = Html.fromHtml(defaultLang?.get_15__finish() + "&#8594;")
        binding.btnQsfinish.text = Html.fromHtml(defaultLang?.get_15__finish() + "&#8594;")
        binding.btnNext.text = Html.fromHtml(defaultLang?.get_15__next() + "&#8594;")
        binding.btnBack.text = Html.fromHtml("&#8592;"+defaultLang?.get_15__back())
        if (sessionManager!!.fundrising_status.equals("0", ignoreCase = true)) {
            val drawable = GradientDrawable()
            drawable.shape = GradientDrawable.RECTANGLE
            drawable.cornerRadius = 13.0f
            drawable.setColor(Color.parseColor(sessionManager!!.topBackColor))
            binding.btnStart.setBackgroundDrawable(drawable)
            binding.btnNext.background = drawable
            binding.btnQsfinish.background = drawable
            binding.btnBack.background = drawable
            binding.btnFinish.background = drawable
            binding.btnFinish.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
            binding.btnBack.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
            binding.btnQsfinish.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
            binding.btnNext.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
            binding.btnStart.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                binding.surveyProgress.progressTintList = ColorStateList.valueOf(Color.parseColor(sessionManager!!.topBackColor))
            } else {
                binding.surveyProgress.indeterminateDrawable?.setColorFilter(Color.parseColor(sessionManager!!.topBackColor), PorterDuff.Mode.SRC_IN)
            }
            //            survey_progress.getProgressDrawable().setColorFilter(Color.parseColor(sessionManager.getTopBackColor()), PorterDuff.Mode.SRC_IN);
        } else {
            val drawable = GradientDrawable()
            drawable.shape = GradientDrawable.RECTANGLE
            drawable.cornerRadius = 13.0f
            drawable.setColor(Color.parseColor(sessionManager!!.funTopBackColor))
            binding.btnStart.setBackgroundDrawable(drawable)
            binding.btnNext.background = drawable
            binding.btnQsfinish.background = drawable
            binding.btnBack.background = drawable
            binding.btnFinish.background = drawable
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                binding.surveyProgress.progressTintList = ColorStateList.valueOf(Color.parseColor(sessionManager!!.funTopBackColor))
            } else {
                binding.surveyProgress.indeterminateDrawable?.setColorFilter(Color.parseColor(sessionManager!!.funTopBackColor), PorterDuff.Mode.SRC_IN)
            }
            binding.btnFinish.setTextColor(Color.parseColor(sessionManager!!.funTopTextColor))
            binding.btnBack.setTextColor(Color.parseColor(sessionManager!!.funTopTextColor))
            binding.btnQsfinish.setTextColor(Color.parseColor(sessionManager!!.funTopTextColor))
            binding.btnNext.setTextColor(Color.parseColor(sessionManager!!.funTopTextColor))
            binding.btnStart.setTextColor(Color.parseColor(sessionManager!!.funTopTextColor))
        }
        surveyDetail()
        text = "<html><body style=\"text-align:justify\"> %s </body></Html>"
        binding.btnQsfinish.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View) {
                if (questionArrayList!![index].q_type.equals("3", ignoreCase = true)) {
                    val editText = binding.linearCustome.findViewWithTag<EditText>("edt")
                    if (editText != null) {
                        questionArrayList!!.get(index).ans = editText.text.toString()
                        if (questionArrayList!![index].ans.length != 0) {
                            try {
                                jsonInput!!.put("User_id", sessionManager!!.userId)
                                jsonInput!!.put("Question_id", questionArrayList!![index].q_id)
                                jsonInput!!.put("Answer", editText.text.toString())
                                Log.d("Json", jsonInput.toString())
                                editText.clearFocus()
                                sessionManager!!.keyboradHidden(editText)
                                if (jsonInput!!.length() != 0) {
                                    ansHashMap!![index.toString() + ""] = jsonInput
                                    //jArrayInput.put(jsonInput);
                                    Log.d("JsonArray", ansHashMap.toString())
                                }
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                        }
                    }
                } else if (questionArrayList!![index].q_type.equals("1", ignoreCase = true)) {
                    if (questionArrayList!![index].ans.length != 0) {
                        try {
                            jsonInput!!.put("User_id", sessionManager!!.userId)
                            jsonInput!!.put("Question_id", questionArrayList!![index].q_id)
                            jsonInput!!.put("Answer", questionArrayList!![index].ans)
                            if (jsonInput!!.length() != 0) {
                                ansHashMap!![index.toString() + ""] = jsonInput
                                //jArrayInput.put(jsonInput);
                                Log.d("JsonArray", ansHashMap.toString())
                            }
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    }
                    Log.d("JsonObject", jsonInput.toString())
                } else if (questionArrayList!![index].q_type.equals("2", ignoreCase = true)) {
                    if (questionArrayList!![index].ans.length != 0) {
                        try {
                            jsonInput!!.put("User_id", sessionManager!!.userId)
                            jsonInput!!.put("Question_id", questionArrayList!![index].q_id)
                            jsonInput!!.put("Answer", arrayAddiChkList)
                            Log.d("JsonObject", jsonInput.toString())
                            if (jsonInput!!.length() != 0) {
                                ansHashMap!![index.toString() + ""] = jsonInput
                                //jArrayInput.put(jsonInput);
                                Log.d("JsonArray", ansHashMap.toString())
                            }
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    }
                    Log.d("JsonObject", jsonInput.toString())
                }
                for (i in 0 until ansHashMap!!.size) {
                    jArrayInput!!.put(ansHashMap!![i.toString() + ""])
                }
                Log.i("JsonArray", "FINAL JASON ARRAY " + jArrayInput.toString())

                //linear_end_page.setVisibility(View.GONE);
                if (questionArrayList!![index].ans.length != 0) {
                    if (sessionManager!!.isLogin) {
                        saveSurvey()
                        if (str_thanku.equals("", ignoreCase = true)) {
                            if (sessionManager!!.isSurveyFromQR) {
//                                GlobalData.CURRENT_FRAG = GlobalData.QrcodeScannerSharecontactFragment
                                GlobalData.CURRENT_FRAG = GlobalData.QrcodeScannerSharecontactFragment
                                (activity as MainActivity?)!!.reloadFragment()
                            }else if (sessionManager!!.IsSurveyFromGemification()){
                                GlobalData.CURRENT_FRAG = GlobalData.Gamification_Fragment
                                var bundle=Bundle()
                                bundle.putInt("curent_item",2)
                                (activity as MainActivity?)!!.reloadFragment(bundle)
                            } else if (sessionManager!!.isSurveyFromContinued) {
                                sessionManager!!.setIsSurveyFromContinued(false)
                                GlobalData.CURRENT_FRAG = GlobalData.ContinuedEducationListFragment
                                (activity as MainActivity?)!!.reloadFragment()
                            } else {
                                if (sessionManager!!.fundrising_status.equals("1", ignoreCase = true)) {
                                    GlobalData.CURRENT_FRAG = GlobalData.Fundrising_Home
                                    (activity as MainActivity?)!!.loadFragment()
                                } else {
                                    if (sessionManager!!.showHomePageFeed.equals("1", ignoreCase = true)) {
                                        GlobalData.CURRENT_FRAG = GlobalData.HOME_FRAGMENT_WITH_FEED
                                    } else {
                                        GlobalData.CURRENT_FRAG = GlobalData.HOME_FRAGMENT
                                    }
                                    (activity as MainActivity?)!!.loadFragment()
                                }
                            }
                        } else {
                            binding.linearQuestionPage.visibility = View.GONE
                            binding.linearEndPage.visibility = View.VISIBLE
                            val pish = "<html><head><style type=\"text/css\">@font-face {font-family: MyFont;src: url(\"file:///android_asset/fonts/Lato_Light.ttf\")}body {font-family: MyFont;font-size: medium;text-align: justify;}</style></head><body>"
                            val pas = "</body></html>"
                            val myHtmlString = pish + str_thanku + pas
                            binding.webViewThanku.loadDataWithBaseURL("file:///android_asset", String.format(text!!, myHtmlString), "text/html; charset=utf-8", "utf-8", null)

                            //   webView_thanku.loadData(String.format(text, str_thanku),"text/html; charset=utf-8","utf-8");
                        }
                    } else {
                        val dialog = AlertDialog.Builder((context)!!)
                        dialog.setTitle(R.string.app_name)
                        dialog.setMessage(R.string.login_message)
                        dialog.setCancelable(false)
                        dialog.setPositiveButton("Ok", DialogInterface.OnClickListener { dialog, which -> dialog.dismiss() })
                        dialog.show()
                        //                        startActivity(new Intent(getActivity(), LoginMainScreen.class));
                    }
                } else {
                    ToastC.show(activity, defaultLang!!._15__PLEASE_SELECT_AN_ANSWER)
                }
            }
        })
        binding.btnStart.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View) {
                Log.d(TAG, "onClick: ${Gson().toJson(questionArrayList)}")
                if (is_view.equals("0", ignoreCase = true)) {
                    if (sessionManager!!.getsurvey_answer_to_gain_points().equals("1", ignoreCase = true)) {
                        binding.linearStartPage.visibility = View.GONE
                        binding.linearQuestionPage.visibility = View.VISIBLE
                    } else {
                        binding.linearNodata.visibility = View.VISIBLE
                        binding.txtNoData.text = message
                        binding.linearStartPage.visibility = View.GONE
                        binding.linearQuestionPage.visibility = View.GONE
                        binding.linearEndPage.visibility = View.GONE
                    }
                } else {
//                    binding.linearNodata.visibility = View.VISIBLE
//                    binding.txtNoData.text = message
//                    binding.linearStartPage.visibility = View.GONE
//                    binding.linearQuestionPage.visibility = View.GONE
//                    binding.linearEndPage.visibility = View.GONE
                    binding.linearStartPage.visibility = View.GONE
                    binding.linearQuestionPage.visibility = View.VISIBLE
                }
            }
        })
        binding.btnFinish.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View) {
                if (GlobalData.isNetworkAvailable(activity)) {
                    if (sessionManager!!.isSurveyFromQR) {
                        GlobalData.CURRENT_FRAG = GlobalData.QrcodeScannerSharecontactFragment
                        (activity as MainActivity?)!!.reloadFragment()
                    } else if (sessionManager!!.isSurveyFromContinued) {
                        sessionManager!!.setIsSurveyFromContinued(false)
                        GlobalData.CURRENT_FRAG = GlobalData.ContinuedEducationListFragment
                        (activity as MainActivity?)!!.reloadFragment()
                    } else {
                        if (sessionManager!!.fundrising_status.equals("1", ignoreCase = true)) {
                            GlobalData.CURRENT_FRAG = GlobalData.Fundrising_Home
                            (activity as MainActivity?)!!.loadFragment()
                        } else {
                            if (sessionManager!!.showHomePageFeed.equals("1", ignoreCase = true)) {
                                GlobalData.CURRENT_FRAG = GlobalData.HOME_FRAGMENT_WITH_FEED
                            } else {
                                GlobalData.CURRENT_FRAG = GlobalData.HOME_FRAGMENT
                            }
                            (activity as MainActivity?)!!.loadFragment()
                        }
                    }
                } else {
                    ToastC.show(activity, getString(R.string.noInernet))
                }
            }
        })
        if (index == 0) {
            binding.btnBack.visibility = View.GONE
        }
        binding.btnBack.setOnClickListener(object : View.OnClickListener {
            @SuppressLint("NewApi")
            override fun onClick(v: View) {
                isNavigatingBack = true
                q_index--
                Log.d("Left Button Clicked", "" + v.id)
//                if (index != 0) {
//                    progress_amount = ((100 / questionArrayList!!.size))
//                    total_progres = total_progres - progress_amount
//                    binding.surveyProgress.progress = total_progres
//                }
                //updateProgress()
                if (binding.btnQsfinish.isShown!!) {
                    binding.btnQsfinish.visibility = View.GONE
                    binding.btnNext.visibility = View.VISIBLE
                }
                if (index == 1) {
                    index--
                    binding.btnBack.visibility = View.GONE
                } else {
                    index--
                }
                jsonInput = JSONObject()
                Log.d("AITL Question_type", questionArrayList!![index].q_type)
                if (questionArrayList!![index].q_type.equals("1", ignoreCase = true)) {
                    binding.linearCustome.removeAllViews()
                    binding.txtHint.text = defaultLang!!.get_15__please_select_one_answer()
                    val radioGroup = RadioGroup(activity)
                    try {
                        val jsonArray = listData!![index.toString() + ""]
                        for (l in 0 until jsonArray!!.length()) {
                            val radioButton = RadioButton(activity)
                            radioButton.text = jsonArray.get(l).toString()
                            radioButton.setPadding(15, 15, 0, 15)
                            radioButton.textSize = 15f
                            if (sessionManager!!.fundrising_status.equals("0", ignoreCase = true)) {
                                radioButton.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
                                radioButton.setBackgroundColor(Color.parseColor(sessionManager!!.topBackColor))
                            } else {
                                radioButton.setTextColor(Color.parseColor(sessionManager!!.funTopBackColor))
                                radioButton.setBackgroundColor(Color.parseColor(sessionManager!!.funTopTextColor))
                            }
                            GlobalData.customeRadioColorChange(radioButton, sessionManager)
                            val params1 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                            params1.leftMargin = 50
                            params1.rightMargin = 50
                            params1.topMargin = 50
                            radioGroup.addView(radioButton)
                            radioButton.layoutParams = params1
                            Log.i("JsonArray", "BACK BUTTON BEAN" + questionArrayList!![index].ans)
                            Log.i("JsonArray", "BACK BUTTON RADIO" + radioButton.text.toString())
                            if (radioButton.text.toString().equals(questionArrayList!![index].ans, ignoreCase = true)) {
                                radioButton.isChecked = true
                            }
                            radioGroup.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener {
                                override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
                                    val rdButton = group.findViewById<RadioButton>(checkedId)
                                    StrRadio = rdButton.text.toString()
                                    questionArrayList!!.get(index).ans = ""
                                    questionArrayList!!.get(index).ans = StrRadio as String
                                    Log.d("JsonArray", "Radio button$StrRadio")
                                }
                            })
                        }
                        binding.linearCustome?.addView(radioGroup)
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                } else if (questionArrayList!![index].q_type.equals("2", ignoreCase = true)) {
                    binding.linearCustome.removeAllViews()
                    arrayAddiChkList!!.clear()
                    binding.txtHint.text = "Please answer in your own words"
                    try {
                        val jsonArray = listData!![index.toString() + ""]
                        for (l in 0 until jsonArray!!.length()) {
                            val chkBox = CheckBox(activity)
                            chkBox.text = jsonArray.get(l).toString()
                            chkBox.setPadding(10, 15, 0, 15)
                            chkBox.textSize = 15f
                            chkBox.id = l
                            if (sessionManager!!.fundrising_status.equals("0", ignoreCase = true)) {
                                chkBox.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
                                chkBox.setBackgroundColor(Color.parseColor(sessionManager!!.topBackColor))
                            } else {
                                chkBox.setTextColor(Color.parseColor(sessionManager!!.funTopBackColor))
                                chkBox.setBackgroundColor(Color.parseColor(sessionManager!!.funTopTextColor))
                            }
                            GlobalData.customeCheckboxColorChange(chkBox, sessionManager)
                            val params1 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                            params1.leftMargin = 50
                            params1.topMargin = 50
                            params1.rightMargin = 50
                            params1.gravity = Gravity.CENTER_VERTICAL
                            chkBox.layoutParams = params1
                            binding.linearCustome.addView(chkBox)
                            if (arrayAddiChkList!!.size != 0) {
                                Log.d("AITL Array", questionArrayList!![index].ans)
                                val strreplace = questionArrayList!![index].ans.replace("[", " ").replace("]", "")
                                Log.d("AITL replace", questionArrayList!![index].ans.replace("[", " ").replace("]", ""))
                                split_chk = strreplace.split(",".toRegex()).toTypedArray()
                                for (z in split_chk!!.indices) {
                                    Log.d("AITL Array String", split_chk!![z])
                                    Log.d("AITL checkbox String", chkBox.text.toString())
                                    if (chkBox.text.toString().equals(split_chk!![z].trim { it <= ' ' }, ignoreCase = true)) {
                                        Log.d("AITL IF Array String", split_chk!![z])
                                        chkBox.isChecked = true
                                    }
                                }
                            } else {
                            }
                            chkBox.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
                                override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
                                    if (isChecked) {
                                        arrayAddiChkList!!.add(chkBox.text.toString())
                                        questionArrayList!!.get(index).ans = ""
                                        questionArrayList!!.get(index).ans = arrayAddiChkList.toString()
                                        //    chkboxArray.add(chkBox);
                                    } else {
                                        arrayAddiChkList!!.remove(chkBox.text.toString())
                                        questionArrayList!!.get(index).ans = ""
                                        questionArrayList!!.get(index).ans = arrayAddiChkList.toString()
                                        //                                           chkboxArray.add(chkBox);
                                    }
                                }
                            })
                        }
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                } else if (questionArrayList!![index].q_type.equals("3", ignoreCase = true)) {

//            ToastC.show(getActivity(),"abc");
                    binding.linearCustome.removeAllViews()
                    binding.txtHint.text = "Please type your answer below"
                    try {
                        val jsonArray = listData!![index.toString() + ""]
                        val edt_ans = EditText(activity)
                        edt_ans?.tag = "edt"
                        for (l in 0 until jsonArray!!.length()) {
                            edt_ans.hint = jsonArray.get(l).toString()
                            edt_ans.setPadding(20, 25, 0, 25)
                            edt_ans.textSize = 15f
                            edt_ans.setText(questionArrayList!![index].ans)
                            edt_ans.minLines = 5
                            edt_ans.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
                            edt_ans.isSingleLine = false
                            edt_ans.imeOptions = EditorInfo.IME_FLAG_NO_ENTER_ACTION
                            edt_ans.setBackgroundResource(R.drawable.square_bg)
                            edt_ans.id = l
                            edt_ans.gravity = Gravity.TOP
                            val params1 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f)
                            params1.topMargin = 20
                            edt_ans.layoutParams = params1
                            binding.linearCustome.addView(edt_ans)

//                    questionArrayList.get(index).setAns("");
//                    questionArrayList.get(index).setAns(edt_ans.getText().toString());
                        }
                        Log.d("JsonObject", jsonInput.toString())
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                }
                //                   Log.d("Right Button Clicked", "" + quesList.get(index).toString());
//                   txt_question.setText(quesList.get(index).toString());
                Log.d("Right Button Clicked", "" + questionArrayList!![index].question)
                binding.txtQuestion.text = questionArrayList!![index].question

                saveProgress()
            }
        })
        binding.btnNext.setOnClickListener(View.OnClickListener { v: View? ->
            isNavigatingBack = false
            q_index++
            if (questionArrayList!!.isEmpty()) {
                ToastC.show(activity, "No Data")
            } else {

                if (questionArrayList!!.get(index).q_type.equals("3", ignoreCase = true)) {
                    val editText: EditText? = binding.linearCustome.findViewWithTag("edt")
                    if (editText != null) {
                        questionArrayList!!.get(index).ans = editText.text.toString()
                        if (questionArrayList!!.get(index).ans.length != 0) {
                            try {
                                jsonInput!!.put("User_id", sessionManager!!.userId)
                                jsonInput!!.put("Question_id", questionArrayList!!.get(index).q_id)
                                jsonInput!!.put("Answer", editText.text.toString())
                                editText.clearFocus()
                                sessionManager!!.keyboradHidden(editText)
                                Log.d("Json", jsonInput.toString())
                                if (jsonInput!!.length() != 0) {
                                    ansHashMap!!.put(index.toString() + "", jsonInput)
                                    //jArrayInput.put(jsonInput);
                                    Log.d("JsonArray", ansHashMap.toString())
                                }
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                        }
                    }
                } else if (questionArrayList!!.get(index).q_type.equals("1", ignoreCase = true)) {
                    if (questionArrayList!!.get(index).ans.length != 0) {
                        try {
                            jsonInput!!.put("User_id", sessionManager!!.userId)
                            jsonInput!!.put("Question_id", questionArrayList!!.get(index).q_id)
                            jsonInput!!.put("Answer", questionArrayList!!.get(index).ans)
                            if (jsonInput!!.length() != 0) {
                                ansHashMap!!.put(index.toString() + "", jsonInput)
                                //jArrayInput.put(jsonInput);
                                Log.d("JsonArray", ansHashMap.toString())
                            }
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    }
                    Log.d("JsonObject", jsonInput.toString())
                } else if (questionArrayList!!.get(index).q_type.equals("2", ignoreCase = true)) {
                    if (questionArrayList!!.get(index).ans.length != 0) {
                        try {
                            jsonInput!!.put("User_id", sessionManager!!.userId)
                            jsonInput!!.put("Question_id", questionArrayList!!.get(index).q_id)
                            jsonInput!!.put("Answer", arrayAddiChkList)
                            Log.d("JsonObject", jsonInput.toString())
                            if (jsonInput!!.length() != 0) {
                                ansHashMap!!.put(index.toString() + "", jsonInput)
                                //jArrayInput.put(jsonInput);
                                Log.d("JsonArray", ansHashMap.toString())
                            }
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    }
                    Log.d("JsonObject", jsonInput.toString())
                }
                if (questionArrayList!!.get(index).ans.length != 0) {
                    index++
                    binding.btnBack.visibility = View.VISIBLE
                    if (index == (questionArrayList!!.size - 1)) {
                        // index = 0;
                        binding.btnNext.visibility = View.GONE
                        binding.btnQsfinish.visibility = View.VISIBLE
                    }
//                    if (index != 0) {
//                        progress_amount = ((100 / questionArrayList!!.size))
//                        total_progres = total_progres + progress_amount
//                        binding.surveyProgress.progress = total_progres
//                    }
                    updateProgress()
                    jsonInput = JSONObject()
                    Log.d("AITL Question_type", questionArrayList!!.get(index).q_type)
                    if (questionArrayList!!.get(index).q_type.equals("1", ignoreCase = true)) {
                        binding.linearCustome.removeAllViews()
                        binding.txtHint.text = defaultLang!!.get_15__please_select_one_answer()
                        val radioGroup: RadioGroup = RadioGroup(activity)
                        try {
                            val jsonArray: JSONArray? = listData!!.get(index.toString() + "")
                            for (l in 0 until jsonArray!!.length()) {
                                val radioButton: RadioButton = RadioButton(activity)
                                radioButton.text = jsonArray.get(l).toString()
                                radioButton.setPadding(15, 15, 0, 15)
                                radioButton.textSize = 15f
                                if (sessionManager!!.fundrising_status.equals("0", ignoreCase = true)) {
                                    radioButton.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
                                    radioButton.setBackgroundColor(Color.parseColor(sessionManager!!.topBackColor))
                                } else {
                                    radioButton.setTextColor(Color.parseColor(sessionManager!!.funTopBackColor))
                                    radioButton.setBackgroundColor(Color.parseColor(sessionManager!!.funTopTextColor))
                                }
                                GlobalData.customeRadioColorChange(radioButton, sessionManager)
                                val params1: LinearLayout.LayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                                params1.leftMargin = 50
                                params1.topMargin = 50
                                params1.rightMargin = 50
                                radioGroup.addView(radioButton)
                                radioButton.layoutParams = params1
                                if (radioButton.text.toString().equals(questionArrayList!!.get(index).ans, ignoreCase = true)) {
                                    radioButton.isChecked = true
                                }
                                radioGroup.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener {
                                    override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
                                        val radioButton: RadioButton = group.findViewById(checkedId)
                                        StrRadio = radioButton.text.toString()
                                        // questionArrayList!!.get(index).ans=""
                                        questionArrayList!!.get(index).ans = StrRadio as String
                                    }
                                })
                            }
                            binding.linearCustome.addView(radioGroup)
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    } else if (questionArrayList!!.get(index).q_type.equals("2", ignoreCase = true)) {
                        binding.linearCustome.removeAllViews()
                        binding.txtHint.text = "Please select Multiple answer."
                        try {
                            val jsonArray: JSONArray? = listData!!.get(index.toString() + "")
                            for (l in 0 until jsonArray!!.length()) {
                                val chkBox: CheckBox = CheckBox(activity)
                                chkBox.text = jsonArray.get(l).toString()
                                chkBox.setPadding(10, 15, 0, 15)
                                chkBox.textSize = 15f
                                chkBox.id = l
                                if (sessionManager!!.fundrising_status.equals("0", ignoreCase = true)) {
                                    chkBox.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
                                    chkBox.setBackgroundColor(Color.parseColor(sessionManager!!.topBackColor))
                                } else {
                                    chkBox.setTextColor(Color.parseColor(sessionManager!!.funTopBackColor))
                                    chkBox.setBackgroundColor(Color.parseColor(sessionManager!!.funTopTextColor))
                                }
                                GlobalData.customeCheckboxColorChange(chkBox, sessionManager)
                                val params1: LinearLayout.LayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                                params1.leftMargin = 50
                                params1.topMargin = 50
                                params1.rightMargin = 50
                                params1.gravity = Gravity.CENTER_VERTICAL
                                chkBox.layoutParams = params1
                                binding.linearCustome.addView(chkBox)
                                if (arrayAddiChkList!!.size != 0) {
                                    Log.d("AITL Array", questionArrayList!!.get(index).ans)
                                    val strreplace: String = questionArrayList!!.get(index).ans.replace("[", " ").replace("]", "")
                                    Log.d("AITL replace", questionArrayList!!.get(index).ans.replace("[", " ").replace("]", ""))
                                    split_chk = strreplace.split(",".toRegex()).toTypedArray()
                                    for (z in split_chk!!.indices) {
                                        Log.d("AITL Array String", split_chk!!.get(z))
                                        Log.d("AITL checkbox String", chkBox.text.toString())
                                        if (chkBox.text.toString().equals(split_chk!!.get(z).trim { it <= ' ' }, ignoreCase = true)) {
                                            Log.d("AITL IF Array String", split_chk!!.get(z))
                                            chkBox.isChecked = true
                                        }
                                    }
                                } else {
                                    Log.d("AITL Array", "Null")
                                }
                                chkBox.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
                                    override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
                                        if (isChecked) {
                                            arrayAddiChkList!!.add(buttonView.text.toString())
                                            questionArrayList!!.get(index).ans = ""
                                            questionArrayList!!.get(index).ans = arrayAddiChkList.toString()
                                            Log.d("AITL", "BEAN CLASS DATA " + questionArrayList!!.get(index).ans)
                                            //    chkboxArray.add(chkBox);
                                        } else {
                                            arrayAddiChkList!!.remove(buttonView.text.toString())
                                            questionArrayList!!.get(index).ans = ""
                                            questionArrayList!!.get(index).ans = arrayAddiChkList.toString()
                                            Log.d("AITL", "BEAN CLASS DATA " + questionArrayList!!.get(index).ans)
                                            //                                           chkboxArray.add(chkBox);
                                        }
                                    }
                                })
                            }
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    } else if (questionArrayList!!.get(index).q_type.equals("3", ignoreCase = true)) {
                        binding.linearCustome.removeAllViews()
                        binding.txtHint.text = "Please type your answer below"
                        try {
                            val jsonArray: JSONArray? = listData!!.get(index.toString() + "")
                            val edt_ans: EditText = EditText(activity)
                            edt_ans.tag = "edt"
                            for (l in 0 until jsonArray!!.length()) {
                                edt_ans.hint = jsonArray.get(l).toString()
                                edt_ans.setPadding(20, 25, 0, 25)
                                edt_ans.textSize = 15f
                                edt_ans.setText(questionArrayList!!.get(index).ans)
                                edt_ans.minLines = 5
                                edt_ans.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
                                edt_ans.isSingleLine = false
                                edt_ans.imeOptions = EditorInfo.IME_FLAG_NO_ENTER_ACTION
                                edt_ans.setBackgroundResource(R.drawable.square_bg)
                                edt_ans.id = l
                                edt_ans.gravity = Gravity.TOP
                                val params1: LinearLayout.LayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f)
                                params1.topMargin = 20
                                edt_ans.layoutParams = params1
                                if (edt_ans.parent != null) {
                                    (edt_ans.parent as ViewGroup).removeView(edt_ans) // <- fix crash
                                }
                                binding.linearCustome.addView(edt_ans)
                            }
                            Log.d("JsonObject", jsonInput.toString())
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                        Log.d("Right Button Clicked", "" + questionArrayList!!.get(index).question)
                    }
                    binding.txtQuestion.text = questionArrayList!!.get(index).question
                } else {
                    ToastC.show(activity, defaultLang!!._15__PLEASE_SELECT_AN_ANSWER)
                }
            }
            saveProgress()
        })
        advertiesment()
        return rootView
    }

    private fun advertiesment() {
        if (GlobalData.isNetworkAvailable(activity)) {
            VolleyRequest(activity, VolleyRequest.Method.POST, MyUrls.getAdvertising, Param.getAdvertisment(sessionManager!!.eventId, sessionManager!!.menuid), 2, false, this)
        } else {
            val cursor = sqLiteDatabaseHandler!!.getAdvertiesMentData(sessionManager!!.eventId, sessionManager!!.menuid)
            Log.d("AITL Cursor Size", "" + cursor.count)
            if (cursor.count > 0) {
                if (cursor.moveToFirst()) {
                    try {
                        val jsonObject = JSONObject(cursor.getString(cursor.getColumnIndex(SQLiteDatabaseHandler.adverties_Data)))
                        Log.d("AITL  Map Oflline", jsonObject.toString())
                        getAdvertiesment(jsonObject)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            }
        }
    }


    private fun getAdvertiesment(jsonObject: JSONObject) {
        try {
            val jsonObjectAdavertiesment = jsonObject.getJSONObject("data")
            val jArrayHeader = jsonObjectAdavertiesment.getJSONArray("header")
            val jArrayFooter = jsonObjectAdavertiesment.getJSONArray("footer")
            topAdverViewArrayList = ArrayList()
            bottomAdverViewArrayList = ArrayList()
            for (i in 0 until jArrayHeader.length()) {
                val index = jArrayHeader.getJSONObject(i)
                topAdverViewArrayList!!.add(
                    AdvertiesmentTopView(
                        index.getString("image"),
                        index.getString("url"),
                        index.getString("id"),
                        index.optString("deeplink"),
                        index.optString("check_deeplink")
                    )
                )
            }

            for (i in 0 until jArrayFooter.length()) {
                val index = jArrayFooter.getJSONObject(i)
                bottomAdverViewArrayList!!.add(
                    AdvertiesMentbottomView(
                        index.getString("image"),
                        index.getString("url"),
                        index.getString("id"),
                        index.getString("deeplink"),
                        index.optString("check_deeplink")
                    )
                )
            }
            if (jsonObjectAdavertiesment.getString("show_sticky").equals("1", ignoreCase = true)) {
                sessionManager!!.footerView(context, "0", binding.MainLayout, binding.relativeLayoutData, binding.linearContent, bottomAdverViewArrayList, activity)
                sessionManager!!.HeaderView(context, "0", binding.MainLayout, binding.relativeLayoutData, binding.linearContent, topAdverViewArrayList, activity)
            } else {
                sessionManager!!.footerView(context, "1", binding.MainLayout, binding.relativeLayoutData, binding.linearContent, bottomAdverViewArrayList, activity)
                sessionManager!!.HeaderView(context, "1", binding.MainLayout, binding.relativeLayoutData, binding.linearContent, topAdverViewArrayList, activity)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    //            Cursor cursor = sqLiteDatabaseHandler.getAttdeeSpeaker_Detail(sessionManager.getEventId(), sessionManager.getUserId(), sessionManager.getEventType(),sessionManager.getCategoryId(), tag);
    private fun surveyDetail() {
        if (GlobalData.isNetworkAvailable(activity)) {

//            Cursor cursor = sqLiteDatabaseHandler.getAttdeeSpeaker_Detail(sessionManager.getEventId(), sessionManager.getUserId(), sessionManager.getEventType(),sessionManager.getCategoryId(), tag);
            val cursor = sqLiteDatabaseHandler!!.getAttdeeSpeaker_Detail(sessionManager!!.eventId, sessionManager!!.userId, sessionManager!!.eventType, sessionManager!!.categoryId, TAG)
            Log.d("AITL Cursor Size", "" + cursor.count)
            if (cursor.count > 0) {
                if (cursor.moveToFirst()) {
                    try {
//                        JSONObject jsonObject = new JSONObject(cursor.getString(cursor.getColumnIndex(sqLiteDatabaseHandler.ListingData)));
                        val jsonObject = JSONObject(cursor.getString(cursor.getColumnIndex(SQLiteDatabaseHandler.AtteendeeSpeakerDetail_Data)))
                        Log.d("AITL  Map Oflline", jsonObject.toString())
                        loadData(jsonObject)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
                if (sessionManager!!.isLogin) {
//                new VolleyRequest(getActivity(), VolleyRequest.Method.POST, MyUrls.getNotification, Param.getNotification(sessionManager.getEventId(), sessionManager.getMenuid(), sessionManager.getUserId(), sessionManager.getToken()),5, false, this);
                    VolleyRequest(activity, VolleyRequest.Method.POST, MyUrls.get_survey,
                        Param.getSurvey(sessionManager!!.eventId, sessionManager!!.eventType,
                            sessionManager!!.userId, sessionManager!!.token, sessionManager!!.categoryId,
                            sessionManager!!.langId), 0, false, this)
                } else {
                    VolleyRequest(activity, VolleyRequest.Method.POST, MyUrls.get_survey,
                        Param.getSurvey(sessionManager!!.eventId, sessionManager!!.eventType, "",
                            sessionManager!!.token, sessionManager!!.categoryId,
                            sessionManager!!.langId), 0, false, this)
                }
            } else {
                if (sessionManager!!.isLogin) {
//                new VolleyRequest(getActivity(), VolleyRequest.Method.POST, MyUrls.getNotification, Param.getNotification(sessionManager.getEventId(), sessionManager.getMenuid(), sessionManager.getUserId(), sessionManager.getToken()),5, false, this);
                    VolleyRequest(activity, VolleyRequest.Method.POST, MyUrls.get_survey,
                        Param.getSurvey(sessionManager!!.eventId, sessionManager!!.eventType,
                            sessionManager!!.userId, sessionManager!!.token, sessionManager!!.categoryId,
                            sessionManager!!.langId), 0, false, this)
                } else {
                    VolleyRequest(activity, VolleyRequest.Method.POST, MyUrls.get_survey,
                        Param.getSurvey(sessionManager!!.eventId, sessionManager!!.eventType, "",
                            sessionManager!!.token, sessionManager!!.categoryId,
                            sessionManager!!.langId), 0, false, this)
                }
            }
        } else {
            val cursor = sqLiteDatabaseHandler!!.getAttdeeSpeaker_Detail(sessionManager!!.eventId, sessionManager!!.userId, sessionManager!!.eventType, sessionManager!!.categoryId, TAG)
            Log.d("AITL Cursor Size", "" + cursor.count)
            if (cursor.count > 0) {
                if (cursor.moveToFirst()) {
                    try {
//                        JSONObject jsonObject = new JSONObject(cursor.getString(cursor.getColumnIndex(sqLiteDatabaseHandler.ListingData)));
                        val jsonObject = JSONObject(cursor.getString(cursor.getColumnIndex(SQLiteDatabaseHandler.ListingData)))
                        Log.d("AITL  Map Oflline", jsonObject.toString())
                        loadData(jsonObject)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            } else {
                ToastC.show(activity, getString(R.string.noInernet))
            }
        }
    }


    @SuppressLint("NewApi")
    private fun loadData(jsonData: JSONObject) {
        try {
            val jArraySurvey = jsonData.getJSONArray("survey")
            message = jsonData.getString("message")
            is_view = jsonData.getString("is_view")
            questionArrayList!!.clear()
            for (j in 0 until jArraySurvey.length()) {
                val jObjectSurvey = jArraySurvey.getJSONObject(j)
                question_id = jObjectSurvey.getString("Question_id")
                question = jObjectSurvey.getString("Question")
                question_type = jObjectSurvey.getString("Question_type")
                val jArrayOption = jObjectSurvey.getJSONArray("Option")
                listData!!["" + j] = jArrayOption
                Log.d("AITL SURVEY OPTION", listData.toString())
                questionArrayList!!.add(Question(question_id!!, question_type!!, question!!, ""))
            }
            loadProgress()
            val jArrayscreen = jsonData.getJSONArray("survey_screens")
            for (i in 0 until jArrayscreen.length()) {
                val jObjectScr = jArrayscreen.getJSONObject(i)
                str_welcome = jObjectScr.getString("welcome_data")
                str_thanku = jObjectScr.getString("thanku_data")
            }
            if (is_view.equals("0", ignoreCase = true)) {
                if (jArrayscreen.length() == 0) {
                    binding.linearNodata.visibility = View.VISIBLE
                    binding.txtNoData.text = jsonData.getString("message")
                    binding.linearStartPage.visibility = View.GONE
                    binding.linearQuestionPage.visibility = View.GONE
                    binding.linearEndPage.visibility = View.GONE
                } else {
                    if (jArraySurvey.length() != 0) {
                        binding.linearNodata.visibility = View.GONE
                        if (jArrayscreen.length() == 0) {
                            binding.linearQuestionPage.visibility = View.VISIBLE
                        } else {
                            if (sessionManager!!.getsurvey_answer_to_gain_points().equals("1", ignoreCase = true)) {
                                binding.linearStartPage.visibility = View.GONE
                                binding.linearQuestionPage.visibility = View.VISIBLE
                            } else {
                                binding.linearNodata.visibility = View.VISIBLE
                                binding.txtNoData.text = jsonData.getString("message")
                                binding.linearStartPage.visibility = View.GONE
                                binding.linearQuestionPage.visibility = View.GONE
                                binding.linearEndPage.visibility = View.GONE
                            }
                        }
                        if (jArraySurvey.length() == 1) {
                            binding.btnNext.visibility = View.GONE
                            binding.btnQsfinish.visibility = View.VISIBLE
                        }
                        jsonInput = JSONObject()
                        Log.d("AITL Question_type", questionArrayList!![index].q_type)
//                        total_progres = 0
//                        progress_amount = ((100 / questionArrayList!!.size))
//                        total_progres = total_progres + progress_amount
                        updateProgress()
                      //  binding.surveyProgress.progress = total_progres
                        if (questionArrayList!![index].q_type.equals("1", ignoreCase = true)) {
                            binding.linearCustome.removeAllViews()
                            binding.txtHint.text = defaultLang!!.get_15__please_select_one_answer()
                            val radioGroup = RadioGroup(activity)
                            try {
                                val jsonArray = listData!![index.toString() + ""]
                                for (l in 0 until jsonArray!!.length()) {
                                    val radioButton = RadioButton(activity)
                                    radioButton.text = jsonArray.get(l).toString()
                                    radioButton.setPadding(15, 15, 0, 15)
                                    radioButton.textSize = 15f
                                    if (sessionManager!!.fundrising_status.equals("0", ignoreCase = true)) {
                                        radioButton.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
                                        radioButton.setBackgroundColor(Color.parseColor(sessionManager!!.topBackColor))
                                    } else {
                                        radioButton.setTextColor(Color.parseColor(sessionManager!!.funTopBackColor))
                                        radioButton.setBackgroundColor(Color.parseColor(sessionManager!!.funTopTextColor))
                                    }
                                    GlobalData.customeRadioColorChange(radioButton, sessionManager)
                                    val params1 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                                    params1.leftMargin = 50
                                    params1.topMargin = 50
                                    radioGroup.addView(radioButton)
                                    radioButton.layoutParams = params1
                                    radioGroup.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener {
                                        override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
                                            val radioButton = group.findViewById<RadioButton>(checkedId)
                                            StrRadio = radioButton.text.toString()
                                            questionArrayList!!.get(index).ans = ""
                                            questionArrayList!!.get(index).ans = StrRadio as String
                                        }
                                    })
                                }
                                binding.linearCustome.addView(radioGroup)
                                jsonInput!!.put("User_id", sessionManager!!.userId)
                                jsonInput!!.put("Question_id", questionArrayList!![index].q_id)
                                jsonInput!!.put("Answer", questionArrayList!![index].ans)
                                Log.d("JsonObject", jsonInput.toString())
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                        } else if (questionArrayList!![index].q_type.equals("2", ignoreCase = true)) {
                            binding.linearCustome.removeAllViews()
                            binding.txtHint.text = "Please select Multiple answer."
                            try {
                                val jsonArray = listData!![index.toString() + ""]
                                for (l in 0 until jsonArray!!.length()) {
                                    val chkBox = CheckBox(activity)
                                    chkBox.text = jsonArray.get(l).toString()
                                    chkBox.setPadding(10, 15, 0, 15)
                                    chkBox.textSize = 15f
                                    chkBox.id = l
                                    if (sessionManager!!.fundrising_status.equals("0", ignoreCase = true)) {
                                        chkBox.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
                                        chkBox.setBackgroundColor(Color.parseColor(sessionManager!!.topBackColor))
                                    } else {
                                        chkBox.setTextColor(Color.parseColor(sessionManager!!.funTopBackColor))
                                        chkBox.setBackgroundColor(Color.parseColor(sessionManager!!.funTopTextColor))
                                    }
                                    GlobalData.customeCheckboxColorChange(chkBox, sessionManager)
                                    val params1 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                                    params1.leftMargin = 50
                                    params1.topMargin = 50
                                    params1.gravity = Gravity.CENTER_VERTICAL
                                    chkBox.layoutParams = params1
                                    binding.linearCustome.addView(chkBox)
                                    if (arrayAddiChkList!!.size != 0) {
                                        Log.d("AITL Array", questionArrayList!![index].ans)
                                        val strreplace = questionArrayList!![index].ans.replace("[", " ").replace("]", "")
                                        Log.d("AITL replace", questionArrayList!![index].ans.replace("[", " ").replace("]", ""))
                                        split_chk = strreplace.split(",".toRegex()).toTypedArray()
                                        for (z in split_chk!!.indices) {
                                            Log.d("AITL Array String", split_chk!![z])
                                            Log.d("AITL checkbox String", chkBox.text.toString())
                                            if (chkBox.text.toString().equals(split_chk!![z].trim { it <= ' ' }, ignoreCase = true)) {
                                                Log.d("AITL IF Array String", split_chk!![z])
                                                chkBox.isChecked = true
                                            }
                                        }
                                    } else {
                                        Log.d("AITL Array", "Null")
                                    }
                                    chkBox.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
                                        override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
                                            if (isChecked) {
                                                arrayAddiChkList!!.add(chkBox.text.toString())
                                                questionArrayList!!.get(index).ans = ""
                                                questionArrayList!!.get(index).ans = arrayAddiChkList.toString()
                                            } else {
                                                arrayAddiChkList!!.remove(chkBox.text.toString())
                                                questionArrayList!!.get(index).ans = ""
                                                questionArrayList!!.get(index).ans = arrayAddiChkList.toString()
                                            }
                                        }
                                    })
                                }
                                jsonInput!!.put("User_id", sessionManager!!.userId)
                                jsonInput!!.put("Question_id", questionArrayList!![index].q_id)
                                jsonInput!!.put("Answer", arrayAddiChkList)
                                Log.d("JsonObject", jsonInput.toString())
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                        } else if (questionArrayList!![index].q_type.equals("3", ignoreCase = true)) {
                            binding.linearCustome.removeAllViews()
                            binding.txtHint.text = "Please type your answer below"
                            try {
                                val jsonArray = listData!![index.toString() + ""]
                                val edt_ans = EditText(activity)
                                edt_ans?.tag = "edt"
                                for (l in 0 until jsonArray!!.length()) {
                                    edt_ans.hint = jsonArray.get(l).toString()
                                    edt_ans.setPadding(20, 25, 0, 25)
                                    edt_ans.textSize = 15f
                                    edt_ans.setText(questionArrayList!![index].ans)
                                    edt_ans.minLines = 5
                                    edt_ans.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
                                    edt_ans.isSingleLine = false
                                    edt_ans.imeOptions = EditorInfo.IME_FLAG_NO_ENTER_ACTION
                                    edt_ans.setBackgroundResource(R.drawable.square_bg)
                                    edt_ans.id = l
                                    edt_ans.gravity = Gravity.TOP
                                    val params1 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f)
                                    params1.topMargin = 20
                                    edt_ans.layoutParams = params1
                                    binding.linearCustome.addView(edt_ans)
                                }
                                Log.d("JsonObject", jsonInput.toString())
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                        }
                        Log.d("Right Button Clicked", "" + questionArrayList!![index].question)
                        binding.txtQuestion.text = questionArrayList!!.get(index).question
                    } else {
                        binding.linearNodata.visibility = View.VISIBLE
                        binding.linearStartPage.visibility = View.GONE
                        binding.linearQuestionPage.visibility = View.GONE
                        binding.linearEndPage.visibility = View.GONE
                    }
//                        binding.linearNodata.visibility = View.VISIBLE
//                        binding.txtNoData.text = jsonData.getString("message")
//                        binding.linearStartPage.visibility = View.GONE
//                        binding.linearQuestionPage.visibility = View.GONE
//                        binding.linearEndPage.visibility = View.GONE

                    binding.linearQuestionPage.visibility = View.GONE
                    binding.linearStartPage.visibility = View.VISIBLE
                    binding.linearNodata.visibility = View.GONE
                    val pish = "<html><head><style type=\"text/css\">@font-face {font-family: MyFont;src: url(\"file:///android_asset/Lato-Light.ttf\")}body {font-family: MyFont;font-size: medium;text-align: left;}</style></head><body>"
                    val pas = "</body></html>"
                    val myHtmlString = pish + str_welcome + pas
                    binding.webViewStart.loadDataWithBaseURL("file:///android_asset", String.format((text)!!, myHtmlString), "text/html; charset=utf-8", "utf-8", null)
                    binding.webViewStart.webViewClient = object : WebViewClient() {
                        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                            clickEvent(url)
                            return true
                        }
                    }

                }
            } else if (jsonData.getString("is_blank").equals("0", ignoreCase = true)) {
                if (jArraySurvey.length() != 0) {
                    binding.linearNodata.visibility = View.GONE
                    if (jArrayscreen.length() == 0) {
                        binding.linearQuestionPage.visibility = View.VISIBLE
                    } else {
                        if (str_welcome.equals("", ignoreCase = true)) {
                            binding.linearStartPage.visibility = View.GONE
                            binding.linearQuestionPage.visibility = View.VISIBLE
                        } else {
                            binding.linearQuestionPage.visibility = View.GONE
                            binding.linearStartPage.visibility = View.VISIBLE
                            val pish = "<html><head><style type=\"text/css\">@font-face {font-family: MyFont;src: url(\"file:///android_asset/Lato-Light.ttf\")}body {font-family: MyFont;font-size: medium;text-align: left;}</style></head><body>"
                            val pas = "</body></html>"
                            val myHtmlString = pish + str_welcome + pas
                            binding.webViewStart.loadDataWithBaseURL("file:///android_asset", String.format((text)!!, myHtmlString), "text/html; charset=utf-8", "utf-8", null)
                            binding.webViewStart.webViewClient = object : WebViewClient() {
                                override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                                    clickEvent(url)
                                    return true
                                }
                            }
                            //webView_start.loadData(String.format(text, str_welcome), "text/html; charset=utf-8","utf-8");
                        }
                    }
                    if (jArraySurvey.length() == 1) {
                        binding.btnNext.visibility = View.GONE
                        binding.btnQsfinish.visibility = View.VISIBLE
                    }
                    jsonInput = JSONObject()
                    Log.d("AITL Question_type", questionArrayList!![index].q_type)
//                    total_progres = 0
//                    progress_amount = ((100 / questionArrayList!!.size))
//                    total_progres = total_progres + progress_amount
//                    binding.surveyProgress.progress = total_progres
                    updateProgress()
                    if (questionArrayList!![index].q_type.equals("1", ignoreCase = true)) {
                        binding.linearCustome.removeAllViews()
                        binding.txtHint.text = defaultLang!!.get_15__please_select_one_answer()
                        val radioGroup = RadioGroup(activity)
                        try {
                            val jsonArray = listData!![index.toString() + ""]
                            for (l in 0 until jsonArray!!.length()) {
                                val radioButton = RadioButton(activity)
                                radioButton.text = jsonArray.get(l).toString()
                                radioButton.setPadding(15, 15, 0, 15)
                                radioButton.textSize = 15f
                                if (sessionManager!!.fundrising_status.equals("0", ignoreCase = true)) {
                                    radioButton.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
                                    radioButton.setBackgroundColor(Color.parseColor(sessionManager!!.topBackColor))
                                } else {
                                    radioButton.setTextColor(Color.parseColor(sessionManager!!.funTopBackColor))
                                    radioButton.setBackgroundColor(Color.parseColor(sessionManager!!.funTopTextColor))
                                }
                                GlobalData.customeRadioColorChange(radioButton, sessionManager)
                                val params1 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                                params1.leftMargin = 50
                                params1.topMargin = 50
                                radioGroup.addView(radioButton)
                                radioButton.layoutParams = params1
                                radioGroup.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener {
                                    override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
                                        val radioButton = group.findViewById<RadioButton>(checkedId)
                                        StrRadio = radioButton.text.toString()
                                        questionArrayList!!.get(index).ans = ""
                                        questionArrayList!!.get(index).ans = StrRadio as String
                                    }
                                })
                            }
                            binding.linearCustome.addView(radioGroup)
                            jsonInput!!.put("User_id", sessionManager!!.userId)
                            jsonInput!!.put("Question_id", questionArrayList!![index].q_id)
                            jsonInput!!.put("Answer", questionArrayList!![index].ans)
                            Log.d("JsonObject", jsonInput.toString())
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    } else if (questionArrayList!![index].q_type.equals("2", ignoreCase = true)) {
                        binding.linearCustome.removeAllViews()
                        binding.txtHint.text = "Please select Multiple answer."
                        try {
                            val jsonArray = listData!![index.toString() + ""]
                            for (l in 0 until jsonArray!!.length()) {
                                val chkBox = CheckBox(activity)
                                chkBox.text = jsonArray.get(l).toString()
                                chkBox.setPadding(10, 15, 0, 15)
                                chkBox.textSize = 15f
                                chkBox.id = l
                                if (sessionManager!!.fundrising_status.equals("0", ignoreCase = true)) {
                                    chkBox.setTextColor(Color.parseColor(sessionManager!!.topTextColor))
                                    chkBox.setBackgroundColor(Color.parseColor(sessionManager!!.topBackColor))
                                } else {
                                    chkBox.setTextColor(Color.parseColor(sessionManager!!.funTopBackColor))
                                    chkBox.setBackgroundColor(Color.parseColor(sessionManager!!.funTopTextColor))
                                }
                                GlobalData.customeCheckboxColorChange(chkBox, sessionManager)
                                val params1 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                                params1.leftMargin = 50
                                params1.topMargin = 50
                                params1.gravity = Gravity.CENTER_VERTICAL
                                chkBox.layoutParams = params1
                                binding.linearCustome.addView(chkBox)
                                if (arrayAddiChkList!!.size != 0) {
                                    Log.d("AITL Array", questionArrayList!![index].ans)
                                    val strreplace = questionArrayList!![index].ans.replace("[", " ").replace("]", "")
                                    Log.d("AITL replace", questionArrayList!![index].ans.replace("[", " ").replace("]", ""))
                                    split_chk = strreplace.split(",".toRegex()).toTypedArray()
                                    for (z in split_chk!!.indices) {
                                        Log.d("AITL Array String", split_chk!![z])
                                        Log.d("AITL checkbox String", chkBox.text.toString())
                                        if (chkBox.text.toString().equals(split_chk!![z].trim { it <= ' ' }, ignoreCase = true)) {
                                            Log.d("AITL IF Array String", split_chk!![z])
                                            chkBox.isChecked = true
                                        }
                                    }
                                } else {
                                    Log.d("AITL Array", "Null")
                                }
                                chkBox.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
                                    override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
                                        if (isChecked) {
                                            arrayAddiChkList!!.add(chkBox.text.toString())
                                            questionArrayList!!.get(index).ans = ""
                                            questionArrayList!!.get(index).ans = arrayAddiChkList.toString()
                                        } else {
                                            arrayAddiChkList!!.remove(chkBox.text.toString())
                                            questionArrayList!!.get(index).ans = ""
                                            questionArrayList!!.get(index).ans = arrayAddiChkList.toString()
                                        }
                                    }
                                })
                            }
                            jsonInput!!.put("User_id", sessionManager!!.userId)
                            jsonInput!!.put("Question_id", questionArrayList!![index].q_id)
                            jsonInput!!.put("Answer", arrayAddiChkList)
                            Log.d("JsonObject", jsonInput.toString())
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    } else if (questionArrayList!![index].q_type.equals("3", ignoreCase = true)) {
                        binding.linearCustome.removeAllViews()
                        binding.txtHint.text = "Please type your answer below"
                        try {
                            val jsonArray = listData!![index.toString() + ""]
                            val edt_ans = EditText(activity)
                            edt_ans?.tag = "edt"
                            for (l in 0 until jsonArray!!.length()) {
                                edt_ans.hint = jsonArray.get(l).toString()
                                edt_ans.setPadding(20, 25, 0, 25)
                                edt_ans.textSize = 15f
                                edt_ans.setText(questionArrayList!![index].ans)
                                edt_ans.minLines = 5
                                edt_ans.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
                                edt_ans.isSingleLine = false
                                edt_ans.imeOptions = EditorInfo.IME_FLAG_NO_ENTER_ACTION
                                edt_ans.setBackgroundResource(R.drawable.square_bg)
                                edt_ans.id = l
                                edt_ans.gravity = Gravity.TOP
                                val params1 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f)
                                params1.topMargin = 20
                                edt_ans.layoutParams = params1
                                binding.linearCustome.addView(edt_ans)
                            }
                            Log.d("JsonObject", jsonInput.toString())
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    }
                    Log.d("Right Button Clicked", "" + questionArrayList!![index].question)
                    binding.txtQuestion.text = questionArrayList!!.get(index).question
                } else {
                    binding.linearNodata.visibility = View.VISIBLE
                    binding.linearStartPage.visibility = View.GONE
                    binding.linearQuestionPage.visibility = View.GONE
                    binding.linearEndPage.visibility = View.GONE
                }
            } else {
                binding.linearNodata.visibility = View.VISIBLE
                binding.txtNoData.text = jsonData.getString("message")
                binding.linearStartPage.visibility = View.GONE
                binding.linearQuestionPage.visibility = View.GONE
                binding.linearEndPage.visibility = View.GONE
            }

            /*else if (jArraySurvey.length() == 1){
                btn_next.setVisibility(View.GONE);
                btn_Qsfinish.setVisibility(View.VISIBLE);
            }*/
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    private fun clickEvent(url: String): Boolean {
        if (Uri.parse(url).scheme.equals(GlobalData.deeplinkHostName, ignoreCase = true)) {
            deepLinkdata(url)
        }
        return true
    }
    private fun deepLinkdata(url: String) {
        val uri = Uri.parse(url)
        val menuId = GlobalData.getDataFromBaseEncrypt(uri.getQueryParameter("node_main"))
        val moduleId = GlobalData.getDataFromBaseEncrypt(uri.getQueryParameter("node_sub"))
        val neventId = GlobalData.getDataFromBaseEncrypt(uri.getQueryParameter("nevent"))
        val nagenda_catId = GlobalData.getDataFromBaseEncrypt(uri.getQueryParameter("node_catid"))
        val node_grpid = GlobalData.getDataFromBaseEncrypt(uri.getQueryParameter("node_grpid"))
        val node_isrgrplist =
            java.lang.Boolean.parseBoolean(GlobalData.getDataFromBaseEncrypt(uri.getQueryParameter("node_isrgrplist")))
        if (sessionManager!!.eventId.equals(neventId, ignoreCase = true)) {
            (activity as MainActivity?)!!.deepLinkRedirectionMethod(
                menuId,
                moduleId,
                nagenda_catId,
                node_grpid,
                node_isrgrplist,
                false
            )
        }
    }
    override fun getVolleyRequestResponse(volleyResponse: VolleyRequestResponse) {
        when (volleyResponse.type) {
            0 -> try {
                val mainObj = JSONObject(volleyResponse.output)
                if (mainObj.getString("success").equals("true", ignoreCase = true)) {
                    val jsonData = mainObj.getJSONObject("data")
                    if (sqLiteDatabaseHandler!!.isAttdeeSpeaker_DetailExist(sessionManager!!.eventId, sessionManager!!.userId, sessionManager!!.eventType, sessionManager!!.categoryId, TAG)) {
                        sqLiteDatabaseHandler!!.deleteAttdeeSpeaker_DetailData(sessionManager!!.eventId, sessionManager!!.userId, sessionManager!!.eventType, sessionManager!!.categoryId, TAG)
                        sqLiteDatabaseHandler!!.insertAttdeeSpeaker_Detail(sessionManager!!.eventId, sessionManager!!.userId, sessionManager!!.eventType, jsonData.toString(), sessionManager!!.categoryId, TAG)
                    } else {
                        sqLiteDatabaseHandler!!.insertAttdeeSpeaker_Detail(sessionManager!!.eventId, sessionManager!!.userId, sessionManager!!.eventType, jsonData.toString(), sessionManager!!.categoryId, TAG)
                    }
                    loadData(jsonData)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            1 -> try {
                val mainObj = JSONObject(volleyResponse.output)
                if (mainObj.getString("success").equals("true", ignoreCase = true)) {
                    val jsonData = mainObj.getJSONObject("data")
                    if (sqLiteDatabaseHandler!!.isAttdeeSpeaker_DetailExist(sessionManager!!.eventId, sessionManager!!.userId, sessionManager!!.eventType, sessionManager!!.categoryId, TAG)) {
                        sqLiteDatabaseHandler!!.deleteAttdeeSpeaker_DetailData(sessionManager!!.eventId, sessionManager!!.userId, sessionManager!!.eventType, sessionManager!!.categoryId, TAG)
                        sqLiteDatabaseHandler!!.insertAttdeeSpeaker_Detail(sessionManager!!.eventId, sessionManager!!.userId, sessionManager!!.eventType, jsonData.toString(), sessionManager!!.categoryId, TAG)
                    } else {
                        sqLiteDatabaseHandler!!.insertAttdeeSpeaker_Detail(sessionManager!!.eventId, sessionManager!!.userId, sessionManager!!.eventType, jsonData.toString(), sessionManager!!.categoryId, TAG)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            2 -> try {
                val jsonObject = JSONObject(volleyResponse.output)
                pagewiseClick()
                if (jsonObject.getString("success").equals("true", ignoreCase = true));
                run {
                    Log.d("AITL Advertiesment", jsonObject.toString())
                    if (sqLiteDatabaseHandler!!.isAdvertiesMentExist(sessionManager!!.eventId, sessionManager!!.menuid)) {
                        sqLiteDatabaseHandler!!.deleteAdvertiesMentData(sessionManager!!.eventId, sessionManager!!.menuid)
                        sqLiteDatabaseHandler!!.insertAdvertiesmentData(sessionManager!!.eventId, sessionManager!!.menuid, jsonObject.toString())
                    } else {
                        sqLiteDatabaseHandler!!.insertAdvertiesmentData(sessionManager!!.eventId, sessionManager!!.menuid, jsonObject.toString())
                    }
                    getAdvertiesment(jsonObject)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    private fun pagewiseClick() {
        if (GlobalData.isNetworkAvailable(activity)) {
            VolleyRequest(activity, VolleyRequest.Method.POST, MyUrls.pageUserClick, Param.pagewiseClick(sessionManager!!.eventId, sessionManager!!.userId, "", "", "", "OT", sessionManager!!.menuid), 6, false, this)
        }
    }

    private fun saveSurvey() {
        VolleyRequest(activity, VolleyRequest.Method.POST, MyUrls.Save_survey, Param.Save_survey(sessionManager!!.eventId, sessionManager!!.userId, sessionManager!!.token, jArrayInput.toString(), sessionManager!!.categoryId, sessionManager!!.isCME, sessionManager!!.cmePoints), 1, true, this)
    }

    override fun onStop() {
        super.onStop()
        GlobalData.IS_SURVEY_VISIBLE=false
        EventBus.getDefault().post(EventBusgetScannData( "done!!"))
    }

    override fun onDestroy() {
        super.onDestroy()
        GlobalData.IS_SURVEY_VISIBLE=false
    }

    private fun loadProgress() {
        val sharedPreferences = activity!!.getSharedPreferences("SurveyProgress", Context.MODE_PRIVATE)
        //val savedCategoryId = sharedPreferences.getString("categoryId", null)

        // Check if the saved category ID matches the current category ID
        val answersJson = sharedPreferences.getString("answers", null)
        if (answersJson != null) {
            val type = object : TypeToken<HashMap<String, ArrayList<Question>>>() {}.type
            categoryMap = Gson().fromJson(answersJson, type)
        }

        // Check if the saved category ID matches the current category ID
        if (categoryMap.containsKey(sessionManager!!.categoryId)) {
            index = sharedPreferences.getInt("currentIndex_${sessionManager!!.categoryId}", 0)
            for(key in categoryMap.keys){
                if (categoryId == key) {
                    tempQuestionList = categoryMap[key]
                    questionArrayList = tempQuestionList
                }
            }
            Log.d("hashmapof", Gson().toJson(categoryMap))
        } else {
            // If category IDs don't match, reset the progress
//            index = 0
//            questionArrayList = ArrayList()
        }

        updateProgress()
    }

    private fun saveProgress() {
        categoryMap[categoryId!!] = questionArrayList!!

        val sharedPreferences = activity!!.getSharedPreferences("SurveyProgress", Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()

        var emptyAnswerIndex = -1
        for (i in questionArrayList!!.indices) {
            if (questionArrayList!![i].ans.isEmpty()) {
                emptyAnswerIndex = i
                break
            }
        }

        if (emptyAnswerIndex != -1) {
            // Save currentIndex specific to the current categoryId
            editor.putInt("currentIndex_${sessionManager!!.categoryId}", emptyAnswerIndex)
        }
        editor.putString("answers",Gson().toJson(categoryMap))
     //   editor.putString("answers", Gson().toJson(questionArrayList))
        editor.apply()
        updateProgress()
    }
    private fun updateProgress() {
        var totalProgress = 0
        val progressAmount = 100 / questionArrayList!!.size

        for (question in questionArrayList!!) {
            if (question.ans.isNotEmpty()) {
                    totalProgress += progressAmount
               // index++
            }
        }
        if (isNavigatingBack && totalProgress > 0) {
            totalProgress -= progressAmount
         //   index--
        }
        binding.surveyProgress.progress = index
    }
}
Leave a Comment