package com.Tricot.calcutator.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.Tricot.calcutator.And_Application;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul10pager;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul11pager;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul12pager;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul1pager;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul2pager;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul3pager;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul4pager;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul5pager;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul6pager;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul7pager;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul8pager;
import com.Tricot.calcutator.Calculator1pager.Calculator1.Calcul9pager;
import com.Tricot.calcutator.CalculatorMenu.Bar_select;
import com.Tricot.calcutator.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;

public class Calculation1_pager extends AppCompatActivity implements OnTimePickerSerListener{
    public static Context mContext;
    EditText ht;
    ViewPager pager;
    public Integer pageNum;
    EditText grams;
    EditText firstgauge;
    EditText paperinchs;
    EditText R_weight;
    EditText C_Round;
    EditText fa21s;
    EditText Gear_A;
    Integer pages;
    Integer bars;
    boolean barmaker;
    boolean barmk;

    ///
    Integer calcul;
    boolean onoff=false;
    Integer cknums=0;
    int cal1_1;
    int cal1_3;
    int cal1_4;
    int priceResult;
    double change_num;
    double numsave;
    float numsave2;
    int ym_switch;
    EditText cal1_1_mil;
    DecimalFormat myFormatter = new DecimalFormat("###,###");
    public void onTimePickerSet(int cal1_1s,int cal1_3s,int cal1_4s){
        cal1_1 = cal1_1s;
        cal1_3 = cal1_3s;
        cal1_4 = cal1_4s;
    }
    public void onbarMakerSet(int cal2_1s){
    }

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator_pager);
        mContext = this;
        final RadioGroup R_group=(RadioGroup)findViewById(R.id.Bar_Group);
        Intent intent = getIntent();
        pages = intent.getExtras().getInt("pages");
        bars= intent.getExtras().getInt("bars");
        barmaker = intent.getExtras().getBoolean("barmaker");
        ym_switch=((And_Application)this.getApplication()).getYm_switch();


        if(ym_switch==0){
            change_num=((And_Application)this.getApplication()).getConversion_ym();
        }else{
            change_num=((And_Application)this.getApplication()).getConversion_my();
        }

        //페이지들 연결
        pager = findViewById(R.id.calcul1vp);
        pager.setOffscreenPageLimit(12);

        MoviePagerAdapter adapter = new MoviePagerAdapter(getSupportFragmentManager());

        Calcul1pager fragment1 = new Calcul1pager();
        adapter.addItem(fragment1);

        Calcul2pager fragment2 = new Calcul2pager();
        adapter.addItem(fragment2);

        Calcul3pager fragment3 = new Calcul3pager();
        adapter.addItem(fragment3);

        Calcul4pager fragment4 = new Calcul4pager();
        adapter.addItem(fragment4);

        Calcul5pager fragment5 = new Calcul5pager();
        adapter.addItem(fragment5);

        Calcul6pager fragment6 = new Calcul6pager();
        adapter.addItem(fragment6);

        Calcul7pager fragment7 = new Calcul7pager();
        adapter.addItem(fragment7);

        Calcul8pager fragment8 = new Calcul8pager();
        adapter.addItem(fragment8);

        Calcul9pager fragment9 = new Calcul9pager();
        adapter.addItem(fragment9);

        Calcul10pager fragment10 = new Calcul10pager();
        adapter.addItem(fragment10);

        Calcul11pager fragment11 = new Calcul11pager();
        adapter.addItem(fragment11);

        Calcul12pager fragment12 = new Calcul12pager();
        adapter.addItem(fragment12);

        pager.setAdapter(adapter);


        pagemove();

        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {
            }

            @Override
            public void onPageSelected(int position)
            {
                if(position==0){
                    if(cal1_1==0){
                        Intent intent = new Intent(getApplicationContext(), Bar_select.class);
                        intent.putExtra("pages",position);
                        intent.putExtra("menu",1);
                        startActivity(intent);
                        finish();
                    }
                }
                if(position==2){
                    if(cal1_3==0){
                        Intent intent = new Intent(getApplicationContext(), Bar_select.class);
                        intent.putExtra("pages",position);
                        intent.putExtra("menu",1);
                        startActivity(intent);
                        finish();
                    }
                }
                if(position==3){
                    if(cal1_4==0){
                        Intent intent = new Intent(getApplicationContext(), Bar_select.class);
                        intent.putExtra("pages",position);
                        intent.putExtra("menu",1);
                        startActivity(intent);
                        finish();
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state)
            {
            }
        });


    }



    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        savedInstanceState.putAll(savedInstanceState);
    }

    //페이지 컨트롤러
    class MoviePagerAdapter extends FragmentStatePagerAdapter {
        ArrayList<Fragment> items = new ArrayList<Fragment>();

        public MoviePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public void addItem(Fragment item){
            items.add(item);
        }

        @Override
        public Fragment getItem(int position) {
            return items.get(position);
        }

        @Override
        public int getCount() {
            pageNum = pager.getCurrentItem();
            return items.size();
        }
    }
    public void pagemove(){
        if(pages!=null){
            barmk=false;
            pager.setCurrentItem(pages,true);
        }
    }


    public void calculation(View v) {

        if (pageNum == 0) {
            calcul = bars;
            float sums = 0;
            float hap = 0;
            float calcusum = 0;
            int milss = 0;
            int rackss = 0;
            int bonss = 0;
            int deniarss = 0;
            int cals1 = 510;
            int cals2 = 520;
            if(onoff==false){
                for(int i = 1; i<=calcul; i++){
                    EditText mil = findViewById(100+i);
                    EditText rack = findViewById(200+i);
                    EditText bon = findViewById(300+i);
                    EditText deniar = findViewById(400+i);
                    TextView calcu1s = findViewById(500 + i);
                    try{
                        String S_mil=mil.getText().toString();
                        String S_rack=rack.getText().toString();
                        String S_bon=bon.getText().toString();
                        String S_deniar=deniar.getText().toString();
                        int mils = Integer.parseInt(S_mil);
                        int racks = Integer.parseInt(S_rack);
                        int bons = Integer.parseInt(S_bon);
                        int deniars = Integer.parseInt(S_deniar);
                        milss = mils;
                        rackss = racks;
                        bonss = bons;
                        deniarss = deniars;
                        float calcu1 = (float)(milss*91.44/480*rackss/9000/1000*bonss*deniarss);
                        float calcu1round = (float) (Math.round(calcu1*100)/100.0);
                        sums = calcu1+sums;
                        String strNum = String.format("%.2f", calcu1round);
                        calcu1s.setText(strNum+"g");
                        System.out.println("=================1=================");
                        System.out.println(strNum);
                        System.out.println("=================1=================");

                    }catch(NumberFormatException e){
                    }catch (NullPointerException e){
                    }
                }
                for(int i=0;i<=calcul;i++){
                    TextView calcu1s = findViewById(500 + i);
                    TextView calcu2s = findViewById(600 + i);
                    try{
                        String C_hap = calcu1s.getText().toString().replaceAll("[^0-9,.]","");
                        System.out.println("=================2=================");
                        System.out.println(C_hap);
                        System.out.println("=================2=================");
                        float calcu1 = Float.parseFloat(C_hap);
                        float calcu2 = calcu1 / sums * 100;
                        float calcu2round = (float) (Math.round(calcu2*100)/100.0);
                        String strNum = String.format("%.2f", calcu2round);
                        calcu2s.setText(strNum+"%");
                        calcusum = calcu2 + calcusum;
                    }catch (Exception e){

                    }

                }
                TextView tvs1 = findViewById(cals1);
                TextView tvs2 = findViewById(cals2);
                float sumsround = (float) (Math.round(sums*100)/100.0);
                String strNum = String.format("%.2f", sumsround);
                tvs1.setText(strNum+"g");
                float calcusumround = Math.round(calcusum);
                int calcusumroundint = (int)calcusumround;
                tvs2.setText(Integer.toString(calcusumroundint)+"%");
                onoff=false;
            }else{
                return;
            }

        }
            if (pageNum == 1) {
                EditText earnings;
                EditText rpm;
                EditText destiny;
                EditText runtime;
                EditText efficiency;
                EditText width_count;
                earnings = findViewById(R.id.day_earnings);
                rpm = findViewById(R.id.day_rpm);
                destiny = findViewById(R.id.day_destiny);
                runtime = findViewById(R.id.day_runtime);
                efficiency = findViewById(R.id.day_efficiency);
                width_count = findViewById(R.id.day_width_count);
                TextView beamweight = findViewById(R.id.day_output);
                TextView totalweight = findViewById(R.id.day_pay);

                try {
                    System.out.println("====================0==========================");
                    float day_earnings = Float.parseFloat(earnings.getText().toString());
                    float day_rpm = Float.parseFloat(rpm.getText().toString());
                    float day_destiny = Float.parseFloat(destiny.getText().toString());
                    float day_runtime = Float.parseFloat(runtime.getText().toString());
                    float day_efficiency = Float.parseFloat(efficiency.getText().toString());
                    float day_width_count = Float.parseFloat(width_count.getText().toString());
                    System.out.println("====================1==========================");

                    double calcul1 = day_rpm * 60 / day_destiny / 91.43 * day_runtime * (day_efficiency / 100) * day_width_count;
                    double calcul2 = day_earnings / calcul1;
                    double calculround1 = Math.round(calcul1);
                    Double calculround1int = calculround1;
                    double calculround2 = Math.round(calcul2);
                    System.out.println("====================2==========================");
                    Double calculround2int = calculround2;

                    String formattedStringPrice = myFormatter.format(calculround2int);

                    beamweight.setText(calculround1int + "yds");
                    totalweight.setText(formattedStringPrice + "원");
                    System.out.println("====================3==========================");
                } catch (NumberFormatException e) {
                    System.out.println("NBFE");
                } catch (NullPointerException e) {
                    System.out.println("NBPE");
                }


            }

        if (pageNum == 2) {
            calcul = bars;
            float sums1= 0;
            float sums2= 0;
            float sums3 = 0;

            try{
                for(int i=1; i<=calcul; i++){
                    EditText hons = findViewById(100+i);
                    EditText pounds = findViewById(200+i);
                    EditText losss = findViewById(300+i);
                    TextView prices = findViewById(400+i);
                    EditText weights = findViewById(R.id.weight);
                    TextView papertotals = findViewById(R.id.papertotal);
                    TextView hontotal = findViewById(R.id.hontotal);
                    TextView poundtotal = findViewById(R.id.poundtotal);
                    TextView barpricetotal = findViewById(R.id.barpricetotal);
                    TextView totals = findViewById(R.id.total);
                    TextView rent = findViewById(R.id.rent);
                    EditText outputs = findViewById(R.id.output);
                    final EditText Earningss = findViewById(R.id.Earnings);
                    EditText salesmargins = findViewById(R.id.salesmargin);


                    try {
                        Float hon = Float.parseFloat(hons.getText().toString());
                        Float pound = Float.parseFloat(pounds.getText().toString());
                        Float loss = Float.parseFloat(losss.getText().toString());
                        Float weight = Float.parseFloat(weights.getText().toString());
                        float calcul1 = (float)(pound*(weight*(hon/100)*(100+loss)/453.592/100));
                        double calculround1 = Math.round(calcul1);
                        int calculround1int = (int)calculround1;
                        sums1 = hon + sums1;
                        sums2 = pound + sums2;
                        sums3 = calcul1 + sums3;
                        String formattedStringPrice = myFormatter.format(calculround1int);
                        prices.setText(formattedStringPrice+"원");
                        /* 합산 부분 반올림*/
                        double sumsround1 = Math.round(sums1);
                        int sumsround1int = (int)sumsround1;
                        double sumsround2 = Math.round(sums2);
                        int sumsround2int = (int)sumsround2;
                        double sumsround3 = Math.round(sums3);
                        int sumsround3int = (int)sumsround3;
                        String formattedStringPrice2 = myFormatter.format(sumsround3int);
                        String formattedStringPrice3 = myFormatter.format(sumsround1int);
                        String formattedStringPrice4= myFormatter.format(sumsround2int);

                        papertotals.setText(formattedStringPrice2+"원/Y");
                        hontotal.setText(formattedStringPrice3+"%");
                        poundtotal.setText(formattedStringPrice4+"원");
                        barpricetotal.setText(formattedStringPrice2+"원");

                        if(i==calcul){
                            Float output = Float.parseFloat(outputs.getText().toString());
                            Float Earnings = Float.parseFloat(Earningss.getText().toString());
                            Float salesmargin = Float.parseFloat(salesmargins.getText().toString());
                            float calcul3 = Earnings/output;
                            double calculround3 = Math.round(calcul3);
                            int calculround3int =(int)calculround3;
                            float calcul2 = sums3 + calcul3+ salesmargin;
                            double calculround2 = Math.round(calcul2);
                            int calculround2int =(int)calculround2;
                            String formattedStringPrice5= myFormatter.format(calculround3int);
                            String formattedStringPrice6= myFormatter.format(calculround2int);
                            rent.setText(formattedStringPrice5+"원");
                            totals.setText(formattedStringPrice6+"원/Y");
                        }
                    }
                    catch (NumberFormatException e){
                    }catch(NullPointerException e){
                    }
                }
            }catch (NumberFormatException e){
            }catch (NullPointerException e){
            }
        }
        if (pageNum == 3) {

            calcul = bars;
            float sums1= 0;
            float sums2= 0;
            float sums3 = 0;
            try{
                for(int i=1; i<=calcul; i++){
                    EditText hons = findViewById(100+i);
                    EditText kgs = findViewById(200+i);
                    EditText losss = findViewById(300+i);
                    EditText weights = findViewById(R.id.weight);
                    TextView totals = findViewById(R.id.total);
                    EditText outputs = findViewById(R.id.output);
                    EditText Earningss = findViewById(R.id.Earnings);
                    TextView rent = findViewById(R.id.rent);
                    TextView papertotals = findViewById(R.id.papertotal);
                    TextView hontotal = findViewById(R.id.hontotal);
                    TextView poundtotal = findViewById(R.id.poundtotal);
                    TextView barpricetotal = findViewById(R.id.barpricetotal);
                    EditText salesmargins = findViewById(R.id.salesmargin);
                    TextView prices = findViewById(400+i);
                    try {
                        Float hon = Float.parseFloat(hons.getText().toString());
                        Float kg = Float.parseFloat(kgs.getText().toString());
                        Float loss = Float.parseFloat(losss.getText().toString());
                        Float weight = Float.parseFloat(weights.getText().toString());
                        float calcul1 = (float)(kg*(100+loss)*(weight*(hon/100))/100000);
                        double calculround1 = Math.round(calcul1);
                        int calculround1int = (int)calculround1;
                        sums1 = hon + sums1;
                        sums2 = kg + sums2;
                        sums3 = calcul1 + sums3;
                        double sumsround1 = Math.round(sums1);
                        int sumsround1int =(int) sumsround1;
                        double sumsround2 = Math.round(sums2);
                        int sumsround2int =(int) sumsround2;
                        double sumsround3 = Math.round(sums3);
                        int sumsround3int =(int) sumsround3;
                        String formattedStringPrice = myFormatter.format(calculround1int);
                        String formattedStringPrice2 = myFormatter.format(sumsround1int);
                        String formattedStringPrice3 = myFormatter.format(sumsround2int);
                        String formattedStringPrice4= myFormatter.format(sumsround3int);

                        prices.setText(formattedStringPrice+"원");
                        hontotal.setText(formattedStringPrice2+"%");
                        poundtotal.setText(formattedStringPrice3+"원");
                        barpricetotal.setText(formattedStringPrice4+"원");
                        papertotals.setText(formattedStringPrice4+"원/Y");
                        if(i==calcul){
                            Float output = Float.parseFloat(outputs.getText().toString());
                            Float Earnings = Float.parseFloat(Earningss.getText().toString());
                            Float salesmargin = Float.parseFloat(salesmargins.getText().toString());
                            float calcul3 = Earnings/output;
                            float calculround3 = Math.round(calcul3);
                            int calculround3int = (int)calculround3;
                            float calcul2 = sums3 + calcul3+ salesmargin;
                            double calculround2 = Math.round(calcul2);
                            int calculround2int = (int)calculround2;
                            /* 합산 부분 반올림 */
                            String formattedStringPrice5= myFormatter.format(calculround3int);
                            String formattedStringPrice6= myFormatter.format(calculround2int);
                            rent.setText(formattedStringPrice5+"원");
                            totals.setText(formattedStringPrice6+"원/Y");
                        }
                    }catch (NullPointerException e){

                    }catch (NumberFormatException e){

                    }
                }
            }catch (NumberFormatException e){
            }catch (Exception e){
            }
        }
            //1-5 계산 버튼 기능
            if (pageNum == 4) {
                ht = findViewById(R.id.sample_horizontal);
                //계산을 위한 입력값 가져오기
                EditText paperin = findViewById(R.id.paper_inch);
                EditText vc = findViewById(R.id.sample_vertical);
                EditText examg = findViewById(R.id.sample_g);
                EditText weights = findViewById(R.id.sample_weight);
                EditText length = findViewById(R.id.sample_length);
                EditText papermil1 = findViewById(R.id.sample_mil_1);
                EditText wellnums = findViewById(R.id.sample_well);
                EditText gaygis = findViewById(R.id.sample_gauge);
                TextView papercalcul1 = findViewById(R.id.paper_weight_view);
                TextView papercalcul2 = findViewById(R.id.paper_weight2_view);
                TextView papermil2 = findViewById(R.id.sample_mil_2_view);
                TextView paperwidth = findViewById(R.id.paper_width_view);


                try {

                    Float paperinch = Float.parseFloat(paperin.getText().toString());
                    Float horizontal = Float.parseFloat(ht.getText().toString());
                    Float vertical = Float.parseFloat(vc.getText().toString());
                    Float semplekg = Float.parseFloat(examg.getText().toString());


                    double calcul1 = paperinch * 2.54 * 91.44 / (vertical * horizontal) * semplekg;
                    Double calculround = Math.round(calcul1 * 10) / 10.0;
                    numsave=calcul1;
                    numsave2=paperinch;


                    papercalcul1.setText(calculround + "g");

                } catch (NumberFormatException e) {
                    System.out.println("Nummmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm");
                }

                try{
                    Float weight = Float.parseFloat(weights.getText().toString());
                    double calcul2 = numsave * ((100 - (weight)) / 100);
                    Double calculround2 = Math.round(calcul2 * 10) / 10.0;
                    papercalcul2.setText(calculround2 + "g");

                }catch (NumberFormatException e){
                    System.out.println("Nummmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm22222");
                }


                try{
                    Float lengths = Float.parseFloat(length.getText().toString());
                    Float parpermil1s = Float.parseFloat(papermil1.getText().toString());
                    double calcul3 = parpermil1s * ((100 - (lengths)) / 100);
                    Double calculround3 = Math.round(calcul3 * 10) / 10.0;
                    papermil2.setText(calculround3 + "cm");
                }catch (NumberFormatException e){


                }

                try{
                    Float wellnum = Float.parseFloat(wellnums.getText().toString());
                    Float gaygi = Float.parseFloat(gaygis.getText().toString());
                    double calcul4 = wellnum * 2.54 * numsave2 / gaygi;
                    Double calculround4 = Math.round(calcul4 * 10) / 10.0;
                    paperwidth.setText(calculround4 + "");
                }catch (NumberFormatException e){

                }
            }


            if (pageNum == 5) {
                grams = findViewById(R.id.gram_1);

            }
            //1-7번 계산식
            if (pageNum == 6) {
                firstgauge = findViewById(R.id.rack_gauge1);


            }
            if (pageNum == 7) {
                paperinchs = findViewById(R.id.paperinch);
                paperinchs.requestFocus();
                try {

                    EditText paperwales = findViewById(R.id.paperwale);
                    EditText paperwidths = findViewById(R.id.paperwidth);
                    TextView papermake = findViewById(R.id.papermakewidth);
                    float paperindch = Float.parseFloat(paperinchs.getText().toString());
                    float paperwale = Float.parseFloat(paperwales.getText().toString());
                    float paperwidth = Float.parseFloat(paperwidths.getText().toString());
                    double calcul = paperindch * 2.54 * paperwale / paperwidth;
                    double calculround = Math.round(calcul * 10) / 10.0;

                    papermake.setText(calculround + "/inch");
                } catch (NumberFormatException e) {
                }
            }
            if (pageNum == 8) {
                R_weight = findViewById(R.id.Raw_weight);
                EditText S_loss = findViewById(R.id.Shearing_loss);
                EditText P_axis = findViewById(R.id.Processing_axis);
                TextView P_weight = findViewById(R.id.Paper_weight);

                try {
                    float paperweight = Float.parseFloat(R_weight.getText().toString());
                    float shring = Float.parseFloat(S_loss.getText().toString());
                    float makeper = Float.parseFloat(P_axis.getText().toString());
                    double calcul = paperweight * ((100 - shring) / 100) * 100 / (100 - makeper);
                    double calculround = Math.round(calcul * 10) / 10.0;

                    P_weight.setText(calculround + "g/yard");
                } catch (NumberFormatException e) {
                }
            }
            if (pageNum == 9) {
                C_Round = findViewById(R.id.Beam_Round);
                C_Round.requestFocus();
                try {
                    TextView C_drum = findViewById(R.id.CornDrum);
                    float Corn_Drum = Float.parseFloat(C_Round.getText().toString());
                    double calcul = Corn_Drum / 3.14 / 25.4;
                    double calculround = Math.round(calcul);
                    int calculroundint = (int)calculround;
                    C_drum.setText(calculroundint + "");
                } catch (NumberFormatException e) {
                }
            }
            if (pageNum == 10) {
                fa21s = findViewById(R.id.a21);


            }
            if (pageNum == 11) {

                Gear_A = findViewById(R.id.A_gear);
                EditText Gear_B = findViewById(R.id.B_gear);
                TextView Density = findViewById(R.id.Density);

                try {
                    String A_Text = Gear_A.getText().toString();
                    String B_Text = Gear_B.getText().toString();

                    float A_Gear = Float.parseFloat(Gear_A.getText().toString());
                    float B_Gear = Float.parseFloat(Gear_B.getText().toString());
                    double calcul = B_Gear / A_Gear * 20.87;
                    double calculround = Math.round(calcul * 100) / 100.0;
                    Density.setText(calculround + "cm");

                } catch (NumberFormatException e) {
                    Toast.makeText(this.getApplicationContext(), "값을 제대로 입력해 주세요", Toast.LENGTH_LONG).show();
                }
            }
        }
        public void capture(View v){
        cknums=cknums+1;
        try {
            View view = findViewById(R.id.captures_1);
            view.buildDrawingCache();
            // 긴 화면도 가져오기 위한 함수 호출

            Bitmap bitmap = loadBitmapFromView(view);

                screenshot(bitmap);
                cknums=0;
                Toast.makeText(this.getApplicationContext(),"저장되었습니다",Toast.LENGTH_SHORT).show();

        }catch (IllegalArgumentException e) {
            e.printStackTrace();
            Toast mToast= Toast.makeText(this.getApplicationContext(),"사진저장에실패하셨습니다",Toast.LENGTH_SHORT);
            mToast.show();
        }

    }
    public static Bitmap loadBitmapFromView(View v){
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }
    public void screenshot(Bitmap bm){
        String extr = Environment.getExternalStorageDirectory().toString();
        File mFolder = new File(extr + "/TexCalculator");
        // 해당 폴더가 없으면 생성
        if (!mFolder.exists()) {
            mFolder.mkdir();
        }
        Random random = new Random();
        String s = "TexCalculator_" + String.valueOf( random.nextInt(99999)) + ".png";
        File file = new File(mFolder.getAbsolutePath(), s);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
